在创建它的Activity被销毁后,Android线程会发生什么? [英] What happens to an Android thread after the Activity that created it is destroyed?

查看:890
本文介绍了在创建它的Activity被销毁后,Android线程会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中,主Activity有时会启动一个Thread来从服务器加载数据。该线程修改应用程序的数据库并编辑一些重要文件。 AFAIK,似乎这个线程继续执行。如果Android进入低内存状态并决定终止整个应用程序,该线程会发生什么?这个线程可能会过早死亡吗?如果是这样,有什么方法可以看出线程被杀死了,并对它做了些什么?

In my Android app, the main Activity will sometimes start a Thread to load data from a server. This thread modifies the app's database and edits some important files. AFAIK, it would appear that this thread continues to execute. What will happen to this thread if the Android gets into a low memory situation and decides to kill the entire Application? Will there ever be a situation were this thread might die prematurely? If so, is there any way I can see that the thread is being killed, and do something about it?

我问,因为这个线程修改了重要数据数据库,如果它突然被杀死,应用程序可能会停止正常运行。

I'm asking because this thread modifies important data in the database, and if it is suddenly killed, the application could stop functioning properly.

推荐答案


AFAIK,似乎这个线程继续执行。

AFAIK, it would appear that this thread continues to execute.

这是真的但你无法保证线程能保持多久。

This is true but you have no guarantee of how long the thread will stay alive.


如果Android进入低内存状态并决定终止整个应用程序,该线程会发生什么?

What will happen to this thread if the Android gets into a low memory situation and decides to kill the entire Application?

根据我的经验,这实际上是一种非常罕见的情况,但它取决于设备的可用内存和用户的行为,例如他们大量使用设备启动多个应用。

This is actually a fairly rare case in my experience but it will depend on the device's available memory and the user's behaviour, for example they use the device heavily and start multiple apps.


此线程是否会出现这种情况?可能会过早死亡?

Will there ever be a situation were this thread might die prematurely?


如果是这样,有什么方法可以看出线程被杀死了,并对它做了些什么?

If so, is there any way I can see that the thread is being killed, and do something about it?


我问,因为这个线程修改了数据库中的重要数据,如果它突然被杀死,应用程序可能会停止正常运行。

I'm asking because this thread modifies important data in the database, and if it is suddenly killed, the application could stop functioning properly.

您所描述的内容可归类为关键任务。正如其他两个答案所指出的那样,服务将是一种更强大的服务方式,因为服务是在低内存情况下被杀死的最后一件事。使用START_REDELIVER_INTENT可能有助于恢复它正在做的事情。

What you describe could be classed as something which is 'mission critical'. As the other two answers have pointed out, a Service would be a more robust way of doing things as a Service is one of the last things to be 'killed' in a low memory situation. Using START_REDELIVER_INTENT may help in resuming what it was doing.

在任何情况下,如果你有关键任务操作,你需要设计你的代码以便完全恢复作为交易的使用和出现错误时回滚的可能性。

In any case, if you have a 'mission critical' operation, you need to design your code for full recovery such as the use of transactions and the possibility of rollbacks in case of errors.

这篇关于在创建它的Activity被销毁后,Android线程会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆