一个服务在后台运行它自己的线程。我想终止那个服务包括它的线程。
我创建了这样的线程并运行它:
public class DaemonService extends Service {
private DaemonThread thread;
class DaemonThread extends Thread {
public void run()
{
runDaemon(mArgv.toArray(), mConfig);
}
}
public void onStart(Intent intent, int startId) {
thread = new DaemonThread();
thread.start();
}
}
如何终止服务并同时终止线程呢?
试用这个方法:
public void stopThread(){
if(myService.getThread()!=null){
myService.getThread().interrupt();
myService.setThread(null);
}
}
在服务中使用onDestroy方法
public void onDestroy(){
thread.stop();
super.onDestroy();
}
再用stopService()方法终止服务。