开发者

Thread after Service is destroyed

I have an and开发者_如何学Pythonroid 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?


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:

Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up an resources it holds (threads, registered receivers, etc) at this point. Upon return, there will be no more calls in to this Service object and it is effectively dead. Do not call this method directly.

I would also suggest that you make your thread use Interrupts so that you can end it gracefully.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜