How to determine if the Android Application was force closed?

有些话、适合烂在心里 提交于 2019-12-20 02:41:53

问题


I need to determine, if my application was force closed, crashed, or terminated normally. My question is: How would you do such a thing in Android environment. Handling exceptions is easy, I only need to Replace Thread.setDefaultExceptionHandler. The hard part is: how can i distinguish a force close shut down from a "normal" (like back press, or home button + lots of time, etc)?

The reason is: We can synchronize the content of the local database, to a server. And we have a good reasons to allow this operation, only in state 1, of the Main activity, and not in state 2. However, if the user finds some db related semantic error in the program, in state 2 of the Main activity, then he or she can stuck there.

We're prepared for incoming call interruption, and other configuration changes, so, if the application gets stuck in state 2, then force closing the app, then reopening it, will still leave the user in stage 2, instead of stage 1.

So we want to leave a way, for the user, after a force closed stuck to go back into stage 1, and be able to synchronize the database, so the majority of his or her work can be saved.


回答1:


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 ..."




回答2:


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.



来源:https://stackoverflow.com/questions/17700542/how-to-determine-if-the-android-application-was-force-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!