2

I am new to android. I want to stop Service from MainActivity. But I am not getting that.while calling the stopService() it display only Toast message. I observed service is still running in back ground. How to stop service. Here is my sample code.

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    // Method to start the service
    public void startService(View view) {
        startService(new Intent(getBaseContext(), MyService.class));
    }
    // Method to stop the service
    public void stopService(View view) {
        stopService(new Intent(getBaseContext(), MyService.class));
    }
}
public class MyService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    static int i=0;
    private static final String Tag="MyService";
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) { 
        new Thread() {
            public void run() {
                while (true) {
                    Log.v(Tag,"Thread"+i);
                }
            }
        }.start()
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
  • see if this topic help you: http://stackoverflow.com/questions/2176375/service-wont-stop-when-stopservice-method-is-called – PedroHawk Jun 24 '14 at 15:42

1 Answers1

1

The service is being stopped if you're seeing your Toast in the onDestroy, but I think you are being confused by the fact that your logging continues. The logging continues because it happens from a separate thread. If you want to make your thread stop as well, you can make a couple of simple changes to your service:

public class MyService extends Service {

    private Thread mThread;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    static int i=0;
    private static final String Tag="MyService";
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) { 
        mThread = new Thread() {
            public void run() {
                while (!interrupted()) {
                    Log.v(Tag,"Thread"+i);
                }
            }
        }.start()
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        mThread.interrupt();
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
}

Note the use of mThread and the checking of interrupted() in the loop. I haven't tested this, but I believe it should work.

HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
  • I'd also wait (join) for the other thread to finish. Or rather, have that thread call stopSelf() once it's done. The stop signal would be sent to the thread itself. – TheRealChx101 Sep 19 '19 at 22:40