I have a service that has its own thread running on background. I'd like to kill that service including the thread.
我有一个在后台运行自己的线程的服务。我想杀掉包括线程在内的那个服务。
I created the thread like this and run it.
我创建了这样的线程并运行它。
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();
}
}
How do I kill the service and the thread as well? I don't have to worry about data safety..
我如何杀死服务和线程?我不必担心数据安全..
5 个解决方案
#1
36
to kill the thread , i think you can do like this :
杀死线程,我想你可以这样做:
myService.getThread().interrupt();
NOTE : the method Thread.stop()
is deprecated
注意:不推荐使用Thread.stop()方法
EDIT : : try this
编辑::试试这个
public void stopThread(){
if(myService.getThread()!=null){
myService.getThread().interrupt();
myService.setThread(null);
}
}
#2
5
The method Thread.stop()
is deprecated, you can use Thread.currentThread().interrupt();
and set thread=null
.
不推荐使用Thread.stop()方法,可以使用Thread.currentThread()。interrupt();并设置thread = null。
#3
1
use Context.stopService()
or stopSelf()
method of the Service.
使用Service的Context.stopService()或stopSelf()方法。
#4
0
Do it in the service's onDestroy method
在服务的onDestroy方法中执行此操作
http://developer.android.com/reference/android/app/Service.html#onDestroy()
public void onDestroy(){
thread.stop();
super.onDestroy();
}
Then stop the service with stopService(); (this will invoke onDestroy());
然后使用stopService()停止服务; (这将调用onDestroy());
#5
0
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Thread.currentThread().interrupt();
}
#1
36
to kill the thread , i think you can do like this :
杀死线程,我想你可以这样做:
myService.getThread().interrupt();
NOTE : the method Thread.stop()
is deprecated
注意:不推荐使用Thread.stop()方法
EDIT : : try this
编辑::试试这个
public void stopThread(){
if(myService.getThread()!=null){
myService.getThread().interrupt();
myService.setThread(null);
}
}
#2
5
The method Thread.stop()
is deprecated, you can use Thread.currentThread().interrupt();
and set thread=null
.
不推荐使用Thread.stop()方法,可以使用Thread.currentThread()。interrupt();并设置thread = null。
#3
1
use Context.stopService()
or stopSelf()
method of the Service.
使用Service的Context.stopService()或stopSelf()方法。
#4
0
Do it in the service's onDestroy method
在服务的onDestroy方法中执行此操作
http://developer.android.com/reference/android/app/Service.html#onDestroy()
public void onDestroy(){
thread.stop();
super.onDestroy();
}
Then stop the service with stopService(); (this will invoke onDestroy());
然后使用stopService()停止服务; (这将调用onDestroy());
#5
0
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Thread.currentThread().interrupt();
}