Where will code stop running when app is killed from task manager on Android

If a user is running my app and decides to kill it via the task manager, are there any guarantees about how much code will be executed before the process dies?

I feel like it could stop running at any arbitrary line, however I guess I don't really know the answer to this.

An example use case is for stopping and restarting a background alarm. If I wanted to cancel an alarm, do some work, then reset the alarm, I feel like there is a chance that the app could stop running after the 'cancel alarm' step but before the 'reset alarm' step. This would leave me in a state that I don't want to be in because I always want my alarm to be set to go off in the future. (Keep in mind this is just an example, I'm not looking for workarounds to this specific case).

Is there anything I can do to ensure that code my java code runs as a 'transaction' similar to a db transaction so that it either all runs or none of it runs?


If a user is running my app and decides to kill it via the task manager, are there any guarantees about how much code will be executed before the process dies?

Not really. If the user elects to use Force Stop in Settings, your process is terminated immediately with extreme prejudice. In other situations where your process is terminated, Android usually will destroy outstanding activities and services, but that is not guaranteed either, particularly if Android is in a rush to free up system RAM (eg, to handle an incoming phone call). Removing the task from the overview screen is not really distinctive here.

Is there anything I can do to ensure that code my java code runs as a 'transaction' similar to a db transaction so that it either all runs or none of it runs?

No.

链接地址: http://www.djcxy.com/p/30494.html

上一篇: 无法从PHP执行Bash脚本

下一篇: 在Android上的任务管理器中杀死应用程序时代码停止运行的位置