2

在我的应用程序中有一个 AsyncTask 在后台运行很长时间。问题是在内存不足的情况下它被VM杀死,有没有办法让应用程序的优先级更高,这样就更难杀死了?我应该改用服务吗?我唯一的目标是让它难以杀死,任何建议或指导将不胜感激,谢谢。

4

3 回答 3

1

我建议您将其作为一项服务和使用startForeground(),这将使其更具抵抗力。但即便如此,它也可能因内存不足而停止。

于 2012-12-03T15:18:56.470 回答
1

不,没有,否则每个人都会滥用它。如果操作系统杀死了您的服务,那么它就是有原因的。如果条件允许并且您的服务符合条件,如果 Android 认为它应该这样做,它将重新启动。

于 2012-12-03T15:31:45.853 回答
0

If your service is killed, then it's restarted by the system as soon as resources allow.

So, make it a service (it shouldn't be a foreground service, if it doesn't do anything decidedly user-oriented like playing music), but a service that can save its state (onMemoryLow() and onDestroy() methods). So after the restart it can resume its process with the user being none the wiser.

于 2012-12-03T15:26:21.653 回答