简体   繁体   English

Android Activity是否会关闭Looper和Handler?

[英]Android Activity will close Looper & Handler?

Sorry but, I'm quite a bit confused after reading this. 抱歉, 看完这篇文章我有点困惑 If let say, I have a single Activity and inside it, I tried to make an inner Class implementing Thread that has looper and handler. 如果说,我有一个Activity,并且在其中,我试图制作一个内部类,该类实现具有循环程序和处理程序的Thread。

My question is; 我的问题是; If I finish() my activity. 如果我完成()我的活动。 Is that close any Looper / Handler i just created last time? 那是否关闭了我上次创建的任何Looper / Handler?

I'm afraid the Thread is still running background altough the Activity is already closed -> ended. 恐怕该线程仍在后台运行,尽管活动已关闭->结束。

After finish, you don't have to care about Handlers attached to the Main Thread, because it's Looper (and the Thread itself) is managed by the system, and it will quit when it is necessary. 完成后,您不必关心附加到主线程上的处理程序,因为它的Looper(和线程本身)是由系统管理的,并且在必要时将退出。

However if the Handler is attached to a separate Looper that is not managed by the system (for example a Thread with a Looper started by you), it will be there in case you have not stopped the relevant Thread (that has the Looper). 但是,如果Handler附加到不受系统管理的单独Looper(例如,您启动了Looper的线程),则在您尚未停止相关线程(具有Looper的线程)的情况下,Handler就会存在。 This true in general for all Threads, the fact that the Thread has a Looper does not change the situation. 通常对于所有线程都是如此,线程具有Looper的事实不会改变这种情况。

So the important think here is to stop every Thread that you started manually. 因此,这里重要的思想是停止手动启动的每个线程。

As an addition: 另外:

  • You can always check your running Threads in Eclipse. 您始终可以在Eclipse中检查正在运行的线程。 Just attach the debugger and go to the Debug view. 只需附加调试器,然后转到“调试”视图。 All Threads will be listed there. 所有线程将在此处列出。
  • Take a look at HandlerThread . 看一下HandlerThread

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM