服务停止时,所有线程都会被杀死吗? [英] Will all threads be killed when a service stopped?

查看:91
本文介绍了服务停止时,所有线程都会被杀死吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务的 onStartCommand()方法中,我启动了一个长时间运行的线程.

In the onStartCommand() method of my service, I start a long-running thread.

该服务在我的 MainActivity onCreate()中启动.

The service is started in my MainActivity's onCreate().

当我的应用程序退出时(用户关闭Main Activity的时间),我停止了该服务.但是我想确保前面提到的线程已被杀死,或者我必须在此之前/之后停止线程.

When my application quit (the time Main Activity closed by user), I stop the service. But I want to ensure that the thread mentioned before has been killed, or I have to stop the thread before/after that.

那么在服务停止时在服务中启动的所有线程会自动被杀死吗?

So will all threads started in a service be killed automatically when the service stopped?

推荐答案

我刚刚通过在服务的 onDestroy()中记录线程的状态来进行检查.我的服务停止后,该线程仍然处于活动状态.

I have just checked this by logging the thread's state in my service's onDestroy(). The thread remains alive after my service stopped.

这篇关于服务停止时,所有线程都会被杀死吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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