• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps service does not stop when asked to stop

tneva82

Newbie
Okay when user presses logout button what's supposed to happen is:

Code:
stopService(myIntent);
           unbindService(mConnection);
            mIsBound = false;

Basically I need to shut down the service on background. So far so good. Except the service restarts itself over and over again! Even if I kill the program from task manager the service restarts itself. Other programs aren't using the service. Heck even when I deinstalled the program the foreground program information popped up indicating it was STILL there(albeit this I solved by rebooting phone).

This is obviously not acceptable behaviour. When user says he's finished with the program 'least he can expect is for the program to stay down rather than keep eating battery by acquiring GPS signal and sending them to server. The program is not intended to be "once you log in you need to deinstal me and reboot phone to get rid of me!" type of program!

Can somebody help me to get the program shut down when asked to?
 
As per develoder.android.com:

Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.

For backwards compatibility, the default implementation calls onStart(Intent, int) and returns either START_STICKY or START_STICKY_COMPATIBILITY.

START_STICKY tells the system to automatically restart the service whenever it is ended.

You should override the onStartCommand() method with something like so:

Code:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
     onStart(intent, startId);
     return START_NOT_STICKY;
}
 
As per develoder.android.com:



START_STICKY tells the system to automatically restart the service whenever it is ended.

You should override the onStartCommand() method with something like so:

Code:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
     onStart(intent, startId);
     return START_NOT_STICKY;
}

Umm. So how does it behave if it's not sticky? It stays on until I myself shut down it? (note whole point of program is kinda moot if the service stops unwantedly all the time. It needs to stay on and active until user specifically requests it to stop)

And why would the default behaviour be one that automatically reboots it even when I try to close the program myself?
 
Yes, START_NO_STICKY stays active until you explicitly shut it down, so you could override the back button functionality to explicitly call stopService().

With that said, a Thread would be a much better choice for what you are going for. Services are meant for long running processes that should last between application runs. If you want their lifetime to coincide with your activities' then you should be using threads instead.
 
Back
Top Bottom