33

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..

codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
  • 1
    Your saying your thread doesn't stop. are you sure the runDaemon(mArgv.toArray(), mConfig); method doesnt spawn another thread inside it? – Blundell May 31 '11 at 11:35

5 Answers5

41

to kill the thread , i think you can do like this :

myService.getThread().interrupt();

NOTE : the method Thread.stop() is deprecated

EDIT : : try this

public void stopThread(){
  if(myService.getThread()!=null){
      myService.getThread().interrupt();
      myService.setThread(null);
  }
}
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • 1
    @LYCSoft, how is the thread still alive? Show us the code in runDaemon(mArgv.toArray(), mConfig); – Blundell May 31 '11 at 14:03
  • 2
    @Houcine can i ask u , which is myService here.. ? – Raghav Chopra Apr 20 '12 at 12:51
  • 1
    @RaghavChopra : myService is your Class that extends the Service , in this case , myService is an instance of the class : DaemonService – Houcine Apr 20 '12 at 13:47
  • @Houcine well i have method name fnExpand how can i kill my thread at the start of methos and its calss name is XYZ – Raghav Chopra Apr 25 '12 at 07:29
  • Can u please have a look on ma code i need 2 kill thread in my uncaughtException Method how can i kill it ? code is in here http://stackoverflow.com/questions/10240962/how-to-handle-the-exception-in-android-with-given-code – Raghav Chopra Apr 25 '12 at 08:42
  • 1
    you need to check for interrupt in the thread if (Thread.interrupted()) { throw new InterruptedException();//or exit etc } – frostymarvelous Aug 29 '14 at 12:38
7

The method Thread.stop() is deprecated, you can use Thread.currentThread().interrupt(); and then set thread=null.

Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63
1
@Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Thread.currentThread().interrupt();
    }
  • 3
    -1 because onDestroy may not always be called, for example; when the System is low on resources it won't call the method. This should be put in onStop. – Loligans Aug 11 '14 at 04:55
  • @Loligans if the system has terminated your app without calling `onDestroy()`, believe me, that thread is not going to keep running. – greeble31 Dec 16 '19 at 14:18
  • is there a way to kill all threads and destroy app at the push of a button? – jvargas Apr 01 '20 at 00:25
1

use Context.stopService() or stopSelf() method of the Service.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
1

Do it in the service's onDestroy method

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());

http://developer.android.com/reference/android/content/Context.html#stopService(android.content.Intent)

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • 3
    the method *Thread.stop()* is deprecated , you can use *Thread.interrupt();* – Houcine May 31 '11 at 11:06
  • Sorry :p switch it out for intterupt then :-) – Blundell May 31 '11 at 11:32
  • 5
    Thread.interrupt() is not necessarily enough... that method will indicate that the thread has been interrupted and some calls will terminate early / immediately due to this flag (as long as it is not cleared), however the thread itself doesn't automatically terminate. The thread needs to have code to detect this condition and terminate itself (returning from the run() method). – mah May 31 '11 at 12:55