Service被销毁后的线程
我有一个名为 MyService
的 Android Service
。
我有一个带有两个按钮(启动服务/停止服务)的Activity
。
当用户单击“启动服务”按钮时,使用 MyService
启动
startService(new Intent(this, MyService.class));
当用户单击“停止服务”按钮时,使用 In MyService
停止
stopService(new Intent(this, MyService.class));
MyService
> 我创建一个 Thread
T
来执行一些长时间运行的任务。
我的问题是:如果 Service
被销毁,那么即使 T
仍在运行,它也会被销毁吗?
I have an android Service
called MyService
.
I have an Activity
with two buttons (start service/stop service).
When user clicks the button "start service", MyService
is started using
startService(new Intent(this, MyService.class));
When user clicks the button "stop service", MyService
is stopped using
stopService(new Intent(this, MyService.class));
In MyService
I create a Thread
T
that performs some long running task.
My question is: If the Service
is destroyed, is T
also destroyed even if it is still running?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,结束您显式创建的线程不是 Android 框架的责任。您需要扩展
onDestroy()
。这是该方法的 javadoc:我还建议您让线程使用中断,以便你可以优雅地结束它。
No, ending a thread you explicitly created is not the responsibility of the Android framework. You need to extend
onDestroy()
. Here is the javadoc for this method:I would also suggest that you make your thread use Interrupts so that you can end it gracefully.