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 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?
I am going to guess that your having a method call for
releaseBind()
means that you previously calledbindService()
on this service and thatreleaseBind()
is callingunbindService()
. If my guess is incorrect, please ignore this answer.A service will shut down after all
bindService()
calls have had their correspondingunbindService()
calls. If there are no bound clients, then the service will also needstopService()
if and only if somebody calledstartService()
on the service.So, there are a few possibilities here:
unbindService()
andstopService()
are asynchronous, something might be going haywire with the timing, in which case you may get better luck if you callstopService()
from yourServiceConnection
'sonServiceDisconnected()
methodAlso, 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 astopDoingStuff()
method through the service binder interface).hai guys sorry for the late answer but as far as i know i have successfull stop the service in this code: you may check a link here.
and in services class:
Are all your bindings closed?
.