3

我需要确定我的应用程序是否被强制关闭、崩溃或正常终止。我的问题是:你会如何在 Android 环境中做这样的事情。处理异常很简单,我只需要替换 Thread.setDefaultExceptionHandler。困难的部分是:我如何区分强制关闭和“正常”(如后按,或主页按钮+大量时间等)?

原因是:我们可以将本地数据库的内容同步到服务器上。我们有充分的理由允许这个操作,只允许在 Main 活动的状态 1,而不是在状态 2。但是,如果用户在程序中发现一些与 db 相关的语义错误,在 Main 活动的状态 2,然后他或她可以卡在那里。

我们已经为来电中断和其他配置更改做好了准备,因此,如果应用程序卡在状态 2,然后强制关闭应用程序,然后重新打开它,仍然会使用户处于第 2 阶段,而不是第 1 阶段。

所以我们想给用户留一条路,在强制关闭卡住后回到第一阶段,并且能够同步数据库,这样他或她的大部分工作都可以节省下来。

4

2 回答 2

3

Unfortunately there is no way. When a user or the system force stops the application, the entire process is simply killed. There is no callback made to inform you that this has happened. Even onDestroy() is not guaranteed to be called. That is documented in the documentation: "There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it ..."

于 2013-07-17T13:09:56.557 回答
0

Perhaps you could write a file on startup that you remove on shutdown. If the app starts and this file is present, you could conclude that it was force-closed.

于 2013-07-17T13:09:41.243 回答