How to stop service by itself?

I start a service in an activity then I want the service to stop itself after awhile.

I called stopSelf() in the service but it doens’t work.

how to make the service stop itself? thanks!!!

How to stop service itself?

I have for example some int myInt in a service and if myInt =1 I’d like to stop service itself. So service should stop automatically. @Override public void onCreate() { super.onCreate(); if(myInt==1){

Can a Windows service stop itself?

I have a Windows service whose startup type is automatic, but I want to do some checks when the service starts, and have the service stop automatically if these checks fail. How can I do this? My ser

How to stop the program itself?

I’m writing method which checks own version of program. If program’s version old, then program need stop itself. How can I stop it?

How to stop a Windows Service programmatically?

I’m writing a simple Windows Service that sends out emails to all employees every month. My question is, how to stop itself when it’s done? I’m a noobie in this field so please help me out. Really app

How to stop current service?

What I am trying to do is if a certain condition is met, stop the service. I can stop a service by ServiceController sc = new ServiceController(serviceName); sc.Stop(); Is there a way to stop the

How to stop android service?

I have created a android service. I start the service and stop the service using startService(intent) and stopService(intent). My problem is that even i stop the service; it starts again without any

How to stop a service?

I have a service [IntentService] which runs a ‘timer task’ scheduled to run after every 5 minutes. I start the service when my application is installed. Now i want to stop my service [service runs a ‘

How to stop service from service thread using C++?

How to stop service from service thread using C++? Service thread runs a conditional while loop. When this condition is false service thread comes out of the loop. But it status shows as Started eve

Can Azure Worker Process stop itself?

Is there a way to stop the worker process by itself? I already coded in console application, which uses REST API to start and stop worker process and delete cloud service deployment. In the latest ann

Dropwizard: How to stop service programmatically

To start the service, I know one uses new MyService().run(args). How to stop it? I need to start and stop programmatically for setUp() and tearDown() in my tests.


If by “doesn’t work” you mean the process doesn’t get killed, then that’s how android works. The System.exit(0) or Process.killProcess(Process.myPid()) will kill your process. But that’s not the Android way of doing things.


By saying “doesn’t work”, I guess you mean that the onDestroy()-method of the service is not invoked.

I had the same problem, because I bound some ServiceConnection to the Service itself using the flag BIND_AUTO_CREATE. This causes the service to be kept alive until every connection is unbound.

Once I change to use no flag (zero), I had no problem killing the service by itself (stopSelf()).

Example code:

final Context appContext = context.getApplicationContext();
final Intent intent = new Intent(appContext, MusicService.class);
ServiceConnection connection = new ServiceConnection() {
  // ...
appContext.bindService(intent, connection, 0);

Killing the service (not process):


Hope that helped.

since you didnt publish your code, i cant know exactly what you are doing, but you must declare WHAT you are stopping:


as in:

public class BatchUploadGpsData extends Service {
    public void onCreate() {
        Log.d("testingStopSelf", "here i am, rockin like a hurricane.   onCreate service");

By calling stopSelf(), the service stops.

Please make sure that no thread is running in the background which makes you feel that the service hasn’t stopped.

Add print statements within your thread.

Hope this helps.

I just ran into the same issue. In my case, I have a singleton service manager that I use to communicate with the service. In the manager the service is started like this:

context.bindService(new Intent(context, MyService.class), serviceConnection, Context.BIND_AUTO_CREATE); 

By removing Context.BIND_AUTO_CREATE as suggested by Alik Elzin, I’ve been able to stop the service using this.stopSelf() and to have onDestroy() called when doing so. This problem is that after that I wasn’t able to restart the service from the manager using the command above.

Finally I’ve fixed this by using a callback from the service that tells the manager to stop the service. This way the manager is always in charge when it comes to start/stop the service and everything seems to work fine. I don’t know if there are any counter indications in doing it this way.

The code is really simple. Create a callback in the service and set it in the manager like this in your connection class:

private ServiceConnection mServiceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        myService = ((MyService.LocalBinder)service).getService();

        myService.setCallback(new MyService.MyServiceCallback() {
            public void onStop() {

    public void onServiceDisconnected(ComponentName className) {
        myService = null;

and stop service:

public void stopService()
    if(mServiceConnection != null){
        try {
        } catch (Exception e) {}

     mContext.stopService(new Intent(mContext, BleDiscoveryService.class));

In the service, simply call myCallback.onStop() when you need to stop it.


Another dirty hack not mentioned here is to throw an exception like NPE. One day I needed to stop InputMethodService and this hack was useful.