6

I have a thread inside a service and I would like to be able to stop the thread when I press the buttonStop on my main activity class.

In my main activity class I have:

public class MainActivity extends Activity implements OnClickListener { 
  ...
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    buttonStart = (Button) findViewById(R.id.buttonStart);
    buttonStop = (Button) findViewById(R.id.buttonStop);

    buttonStart.setOnClickListener(this);
    buttonStop.setOnClickListener(this);
  }

  public void onClick(View src) {
    switch (src.getId()) {
    case R.id.buttonStart:
         startService(new Intent(this, MyService.class));
         break;
    case R.id.buttonStop:
         stopService(new Intent(this, MyService.class));
         break; 
    }           
  }
}

And in my service class I have:

public class MyService extends Service {
  ... 
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

 @Override
 public void onCreate() {
    int icon = R.drawable.myicon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,  notificationIntent, 0);
    notification.setLatestEventInfo(this, "notification title", "notification message", pendingIntent);     
    startForeground(ONGOING_NOTIFICATION, notification);
            ...
 } 

 @Override
 public void onStart(Intent intent, int startid) {
   Thread mythread= new Thread() { 
   @Override
   public void run() {
     while(true) {
               MY CODE TO RUN;
             }
     }
   }
 };
 mythread.start();
}

}

what is the best way to stop the mythread?

Also is the way that I have stopped the service by stopService(new Intent(this, MyService.class)); correct?

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
TJ1
  • 7,578
  • 19
  • 76
  • 119

2 Answers2

9

You can't stop a thread that has a running unstoppable loop like this

while(true)
{

}

To stop that thread, declare a boolean variable and use it in while-loop condition.

public class MyService extends Service {
      ... 
      private Thread mythread;
      private boolean running;



     @Override
     public void onDestroy()
     {
         running = false;
         super.onDestroy();
     }

     @Override
     public void onStart(Intent intent, int startid) {

         running = true;
       mythread = new Thread() { 
       @Override
       public void run() {
         while(running) {
                   MY CODE TO RUN;
                 }
         }
       };
     };
     mythread.start();

}
  • how can I change the boolean variable when I press the `buttonStop` and pass it to the servive? – TJ1 Feb 05 '13 at 06:06
  • You don't need to do that, by calling `stopService()`, `onDestroy()` of Service will be called then, set the boolean will be false –  Feb 05 '13 at 06:07
  • Actually I need to be able to stop the code that I am running (`MY CODE TO RUN`), so I need to be able to change the `running` when I press `buttonStop`. – TJ1 Feb 05 '13 at 06:10
  • You can do it by doing remote-service. –  Feb 05 '13 at 06:26
  • does this mean I have to change the implementation of the service? Isn't there a way to pass a variable from my activity to my service? – TJ1 Feb 05 '13 at 07:03
  • Shouldn't private boolean running be also VOLATILE? – Marian Paździoch Feb 25 '15 at 10:50
-3

You call onDestroy() method for stop service.