views:

1813

answers:

2

Hi all,

I currently have a Service that runs fine when I start it but when I try to stop it using the stopService method its onDestroy method doesn't get called.

Here is the code I use to try to stop the Service

   stop_Scan_Button = (Button) findViewById(R.id.stopScanButton);

    stop_Scan_Button.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Log.d("DEBUGSERVICE", "Stop Button pressed");
            Intent service = new Intent(CiceroEngine.CICERO_SERVICE);
            releaseBind();
            Log.d("Stop_Scan_Button", "Service : " + service.toString());
            stopService(service);
            Log.d("Stop_Scan_Button", "Service should stop!");

            }   
    });

Am I right in thinking that when stopService is used it calls the onDestroy method of the Service? When I press my stop scan button the onDestroy() method in my Service is not called.

Is there anything else I am missing that I should put in to help stop the service?

EDIT: to add onServiceConnected() gets called when stopService is run instead of onServiceDisconnected(), why would that be happening?

EDIT: To add more info regards Binding

I call bindService in the onCreate() method and I then have the releaseBind() method unbind the Service.

Here is the code for that method:

 public void releaseBind(){
    unbindService(this);
  }

So I presume that the unbinding is not my problem?

+2  A: 

I am going to guess that your having a method call for releaseBind() means that you previously called bindService() on this service and that releaseBind() is calling unbindService(). If my guess is incorrect, please ignore this answer.


A service will shut down after all bindService() calls have had their corresponding unbindService() calls. If there are no bound clients, then the service will also need stopService() if and only if somebody called startService() on the service.

So, there are a few possibilities here:

  1. You still have bound clients (e.g., other activities), in which case you cannot stop the service until they unbind
  2. Since both unbindService() and stopService() are asynchronous, something might be going haywire with the timing, in which case you may get better luck if you call stopService() from your ServiceConnection's onServiceDisconnected() method

Also, bear in mind that the exact timing of the service being destroyed is up to Android and may not be immediate. So, for example, if you are relying upon onDestroy() to cause your service to stop some work that is being done, consider using another trigger for that (e.g., activity calling a stopDoingStuff() method through the service binder interface).

CommonsWare
I have edited my original question to include info about my binding, I do have it binding and then unbinding
Donal Rafferty
Ummm, that code is totally messed up and is almost certainly contributing to your problem. `bindService()` and `unbindService()` are asynchronous and are guaranteed to fail the way you are using them. Unfortunately, I have no idea what you are trying to accomplish here, so I have no good way of advising you further.
CommonsWare
Its just a two button UI, one to start the service and one to stop it.I have taken the if statement out of my releaseBind() method leaving just unBindService(this); This appears to unregister the Service as when I click on it a second time I get the "Service not registered" error. However, stopService still doesn't appear to call the onDestroy() method in my Service, the background services still function
Donal Rafferty
Thanks for the input, I have it working perfectly now, thanks for pointing out the flaw in my coding.
Donal Rafferty
+2  A: 

Are all your bindings closed?

A service can be used in two ways. The two modes are not entirely separate. You can bind to a service that was started with startService(). For example, a background music service could be started by calling startService() with an Intent object that identifies the music to play. Only later, possibly when the user wants to exercise some control over the player or get information about the current song, would an activity establish a connection to the service by calling bindService(). In cases like this, stopService() will not actually stop the service until the last binding is closed

.

JRL
Yeh I only bind it once and then call releaseBind() to unbind
Donal Rafferty