1

当 Android(2.3 版)的应用程序内存太少时。当我打开浏览器并打开四张有 30-40MB 可用内存的卡时,我在具有 140 MB 可用内存的设备上进行了测试。我的应用程序是 10 MB,但如果你开始使用另一个 Android 本身它关闭。是否可以强制 Android 从不关闭应用程序?这太烦人了,以至于我在这个应用程序和会话变量(ApplicationState)返回后开始发生奇怪的事情。

4

3 回答 3

3

只有通过修改 android 系统本身(即重建或至少生根)才能完全防止这种情况发生。即便如此,在内存压力的某个点上,阻止您的应用程序被杀死会转化为崩溃用户在前台尝试执行的任何操作,甚至是整个设备。

正如其他人已经建议的那样,您最好根据活动生命周期正确处理被杀死的可能性。

于 2012-06-21T13:49:53.687 回答
2

简单的答案是“这是不可能的”。您可以尝试使用前台服务,这里是一个示例,但即便如此,您也不能保证您的应用程序不会被杀死。如果 android 确实需要内存来存储更重要的应用程序,例如电话。

与其尝试做您所描述的事情,我建议您实际上仔细查看Activity 生命周期并实施适当的回调来保存和恢复您的会话/状态。

于 2012-06-21T13:44:31.740 回答
1

No it isn't possible. The best you can do is use a foreground service, but even that doesn't guarantee your app won't be killed.

It sounds though like you might really want to just store the state of the app so that when you restart it's in the same state the user left it in. If you properly store and restore the application state you shouldn't get strange things happening.

Do you understand the Activity lifecycle? If not, I suggest you read up on it.

于 2012-06-21T13:42:00.417 回答