i have a background service on my android APP that is getting my GPS position and sending it to a remote db. It work's fine.
The problem is when i want to stop the service.... it doesn't stops :S. Also no exception or errors on logcat have appeared... it simply doesn't stops.
this is the code to start my srvice (with a button):
startService(new Intent(GPSLoc.this, MyService.class)); //enciendo el service
this is the code where I stop it (on the onactivityresult method):
stopService(new Intent(GPSLoc.this, MyService.class));
I have been debugged the app, and i checked that the stopService codeline has been called every time that i debugged it, but it doesn't stops......
i am sure that it's not stopped cause on my database i still recive gps positions from the emulator when i have press the button to stop the service.
what i am doing bad?
For those who want to send a request to server periodically, this is my solution. You should have this in your Activity or Fragment Activity
If you are tracking GPS location, you probably used
GoogleApiClient
.The concept is that the Service WILL NOT stop,
if an
GoogleApiClient
instance is still connected within it.(Or any other issue that need to be destroyed / unregistered first)
So to make it works, implement
onDestroy()
within your service:In my case the
stopService
is called withstartService
almost simultaneously so no service is there to be stopped. Try delaystopService
for a few seconds. :)@Override
It's very common this situation where I need to stop my service before to finish the process. In some case is not enough with stopService(intent). You should have in mind the onDestroy() implement in my service. Example:
In this way, when you have stopped the service using stopService method also you will have stopped the process o counter.
Take care! @yaircarreno
my problem solved by removing the added views to
WindowManager
ondestroy