Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stopService doesn't stop's my service.... why?

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?

like image 666
NullPointerException Avatar asked Dec 17 '10 12:12

NullPointerException


People also ask

How to stop service in android studio?

To start the service, call startService(intent) and to stop the service, call stopService(intent) .

How do you stop a service from 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.


2 Answers

Have you implemented onDestroy()? If not, I believe that might be the solution - and you stop your Timer or whatever you're using to run the service within onDestroy().

A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().

See this link for some more information.

like image 61
Joakim Berglund Avatar answered Sep 24 '22 15:09

Joakim Berglund


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.

You probably are not unregistering your LocationListener.

like image 44
CommonsWare Avatar answered Sep 24 '22 15:09

CommonsWare