views:

314

answers:

3

I have an activity with two buttons, start and stop. If the user press the start button a service is created using Context.startService. And the stop button calls Context.stopService.

I want the stop button to be the only way to destroy the service. Now, if i end the activity using a task manager, the service is killed as well. Is there any way to avoid this?

EDIT: Also, i would like to know how a task manager (such as Advanced Task Manager Free) kills activities and services? Since none of my onDestroy() and such methods is called.

A: 

interesting, need same info coincidentally. will watch for an answer here

bob
A: 

I should imagine there isn't a way to do it, otherwise it would be impossible to end the service as the button that stops it would be destroyed. I think all you could do is close everything else in the activity but leave it running to keep the service up.

AaronM
+1  A: 

Now, if i end the activity using a task manager, the service is killed as well. Is there any way to avoid this?

No. Also bear in mind the user can stop your service whenever they want through the Settings application built into Android.

Also, i would like to know how a task manager (such as Advanced Task Manager Free) kills activities and services?

See ActivityManager#restartPackage(). Bear in mind that the behavior of this method may change in the future.

CommonsWare
Okay. Well, thanks! But i know a few application that seems to be doing this. For example Lookout (http://www.androlib.com/android.application.com-lookout-zznx.aspx). If i kill the activity, i can still see Lookout's NotificationService running. Same with gesture search. How do they accomplish this?
shuwo
That would be a fine question to ask the authors of Lookout. I am not one of them. Moreover, it is important for users to maintain control over their devices. Any service that cannot be stopped, at least by the Settings application, is bad for Android. After all, whatever technique they are using could be exploited by malware itself. So, while I think task killers are definitely over-used, everlasting services are worse.
CommonsWare
@shuwo They may have separated the activity and service into separate processes.
jleedev
It sounds like their using a Remote Service