-1

I have tried to stop my service, even my destroy method also executed but my service is not stopping. I have done my best. Please help me.

my code is mentioned below:

MainActivity

  1. Intent intent=new Intent(this,Myservice.class);
    startService(intent); //in start button.

  2. Intent intent=new Intent(this,Myservice.class);
    stopService(intent); //in stop button.

Myservice

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

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this,"runing",Toast.LENGTH_LONG).show();

     {
         myTherad th=new myTherad();
         Thread thread=new Thread(th);
         thread.start();


     }

    return super.onStartCommand(intent, flags, startId);

}

@Override
public void onDestroy() {
    Toast.makeText(this,"stop",Toast.LENGTH_LONG).show();
    super.onDestroy();

}

class myTherad implements Runnable {

    @Override
    public void run() {
        synchronized (this)
        {
            try {

                for(int i=0;i<10;i++)
                {
                    wait(1000);
                    Log.d("mfnhsdgjkfn","===="+String.valueOf(i));
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        stopSelf();


    }


}

}

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 2
    Possible duplicate of [How to stop running services?](https://stackoverflow.com/questions/9964356/how-to-stop-running-services) – AskNilesh Dec 22 '17 at 06:48
  • See [this answer](https://stackoverflow.com/a/47806235/4168607). – ADM Dec 22 '17 at 06:52
  • stopService(new Intent(this, MyService.class)); and handle the service stop process in OnDestroy method of service if needed – abhil nair Dec 22 '17 at 06:54
  • Possible duplicate of [stop service in android](https://stackoverflow.com/questions/5555765/stop-service-in-android) – Tomin B Azhakathu Dec 22 '17 at 06:55
  • 1
    Just to check- you understand that stopping a service does NOT stop thread started by your service, right? The code to stop services has no idea threads exist. You have to do that yourself. – Gabe Sechan Dec 22 '17 at 07:05

2 Answers2

0

I think your service is stopped, but not your Thread. Cancel your Thread as well in onDestroy of your service by introducing for e.g. some kind of state variable:

 class myTherad implements Runnable {
     // Indicates that the runnable should stop. 
     private boolean shouldStop = false;

     @Override
     public void run() {
        synchronized (this)
        {
            try {

                for(int i=0;i<10 && !shouldStop;i++)
                {
                    wait(1000);
                    Log.d("mfnhsdgjkfn","===="+String.valueOf(i));
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        stopSelf();


  }

and declare your runnable as class attribute:

private myTherad runnable;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    runnable=new myTherad();
    Thread thread=new Thread(runnable);
    thread.start();

    return super.onStartCommand(intent, flags, startId);

} 

and stop the runnable within your onDestroy:

@Override
public void onDestroy() {
    Toast.makeText(this,"stop",Toast.LENGTH_LONG).show();
    runnable.shouldStop = true;
    super.onDestroy();

}

At least after the latest wait was executed, your thread should stop. You can optimize that, with an interruption.

Christopher
  • 9,682
  • 7
  • 47
  • 76
0

We have to inturrpt thread that is running , thread.interrupt() will do that and give java.lang.InterruptedException , Try below code :

public class MyService extends Service {
public MyService() {}
myTherad th;
Thread thread;

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

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this,"runing",Toast.LENGTH_LONG).show();

    {
         th=new myTherad();
         thread=new Thread(th);
         thread.start();
    }
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this,"stop",Toast.LENGTH_LONG).show();
    thread.interrupt();
}

class myTherad implements Runnable {

    @Override
    public void run() {
        synchronized (this)
        {
            try {

                for(int i=0;i<10;i++)
                {
                    wait(1000);
                    Log.d("LOG","===="+String.valueOf(i));
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        stopSelf();
    }
}
}
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71