问题描述
我对的AsyncTask和线程的生命周期有点困惑。
当OS进入的OnStart /调用OnStop和onResume /序列的onPause发生一个进程中的线程什么。
受此序列中的线程。
我问这个问题,因为我使用的线程插槽和杀死线程将杀死TCPIP连接以及(我假设)。
如果thredas没有然后杀害我该怎么办'特别重新'给他们,因为他们可能会通过一个处理程序连接到一个活动。
I am a little confused about AsyncTask and Thread life cycle.What happens to a processes threads when the OS enters the OnStart/OnStop and onResume/onPause sequences.Are the threads affected by this sequence.I ask this question because I am using sockets in the threads and killing the threads will kill the tcpip connections as well (I assume).If the thredas are not killed then how do I 'reconnect' to them especially as they may will be 'connected' to an activity via a Handler.
推荐答案
线程,当应用程序去的onStop或在onPause不会自动死亡,但是,也不能保证它的不会被杀死。直到它需要一个线程使用的资源Android将保持运行。
Threads are not automatically killed when the app goes to onStop or onPause, however, there is no guarantee it won't be killed. Android will keep it running until it needs the resources that the thread is using.
一般来说,如果你想有一个持久连接时活动
关闭,然后将其在服务
。您可以运行在它的线程就像一个活动(即通常的连接发生时prevent阻塞),它是不太可能Android将其杀死。
Generally, if you want a persistent connection when an Activity
closes, then put that in a Service
. You can run threads in it just like an Activity (to prevent blocking that usually occurs with connections), and it's far less likely Android will kill it.
这篇关于Android的AsyncTask的和线程的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!