hellosheikh
hellosheikh

Reputation: 3015

android - how can I stop the thread inside the service?

I have a checked button in my MainActivity. If that button is checked it should start the service but if a user unchecked the button I want to stop the service.

So in uncheck condition I have written this stopService(intentname); but the problem is the service is not stopping. Here is my code snippet:

Service Class

public class SimpleService extends Service 
{
    String selectedAudioPath = "";
    private MyThread myythread;
    public Intent intent;
    public boolean isRunning = false;

    long interval=30000;

    @Override
    public IBinder onBind(Intent arg0) 
    {
        return null;
    }

    @Override
    public void onCreate() 
    {
        super.onCreate();
        myythread  = new MyThread(interval);
    }

    @Override
    public synchronized void onDestroy() 
    {
        super.onDestroy();

        if(!isRunning)
        {
            myythread.interrupt();
            myythread.stop();
            isRunning = false;
        }
    }

    @Override
    public synchronized void onStart(Intent intent, int startId) 
    {
        super.onStart(intent, startId);

        if(!isRunning)
        {
            //this.intent = intent;
            //System.out.println("the intent is" + intent);
            myythread.start();
            isRunning = true;
        }
    }

    class MyThread extends Thread
    {
        long interval;
        public MyThread(long interval)
        {
            this.interval=interval;
        }

        @Override
        public void run()
        {
            while(isRunning)
            {
                System.out.println("Service running");
                try 
                {
                    String myString = intent.getStringExtra("name");
                    if(myString == null)
                        Log.d("Service","null");
                    else
                    {
                        Log.d("Service","not null");
                        if(myString.equalsIgnoreCase("image"))
                        {
                            uploadImages();
                            Thread.sleep(interval);
                        }
                        else if(myString.equalsIgnoreCase("audio"))
                        {
                            uploadAudio();
                            Thread.sleep(interval);
                        }
                    }
                } 
                catch (InterruptedException e) 
                {
                    isRunning = false;
                    e.printStackTrace();
                }
            }
        }

Upvotes: 0

Views: 1791

Answers (2)

Rajesh Batth
Rajesh Batth

Reputation: 1662

Don't use Threads. Use AsyncTask instead.

public class MyService extends Service {

    private AsyncTask<Void,Void,Void> myTask;

    @Override
    public void onDestroy(){
         super.onDestroy();
         myTask.cancel(true);
    }

    @Override
    public void onStart(Intent intent, int startid) {
        myTask = new AsyncTask<Void,Void,Void>(){
            @Override
            public void doInBackground(Void aVoid[]){
                doYourWorkHere();
            }
        }
        myTask.execute();
    }

}

Upvotes: 0

Hassaan Rabbani
Hassaan Rabbani

Reputation: 2465

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

}

Source: Stopping a thread inside a service

Upvotes: 1

Related Questions