0

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();
                }
            }
        }
Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
hellosheikh
  • 2,929
  • 8
  • 49
  • 115
  • Possible duplicate of http://stackoverflow.com/questions/4756862/how-to-stop-a-thread and http://stackoverflow.com/questions/5660097/stopping-destroying-a-thread – Ende Neu Feb 24 '14 at 07:39
  • possible duplicate of [Stopping a thread inside a service](http://stackoverflow.com/questions/14700936/stopping-a-thread-inside-a-service) – Hassaan Rabbani Feb 24 '14 at 08:27
  • 1
    It's probably because you copy pasted only parts of your code, but i don't sse why you need a thread in your example : The service itself is already running in a background thread, you can use it directly to upload your file (as it seems from your code you have only one thread to upload) and you don't need to start another thread for that. Besides, your while loop in the thread will indefinitely upload the same file again and again. – Pierre Rust Feb 24 '14 at 08:34
  • Is the `!` in `if(!isRunning)` really intentional? – laalto Feb 24 '14 at 09:50
  • @laalto this is my first time i am using ..i just followed the tutorial – hellosheikh Feb 24 '14 at 12:37

2 Answers2

1

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

Community
  • 1
  • 1
Hassaan Rabbani
  • 2,469
  • 5
  • 30
  • 55
0

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

}
Rajesh Batth
  • 1,672
  • 2
  • 19
  • 23