Make service indepented from activity (Android)
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 serv开发者_Python百科ice 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.
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.
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.
精彩评论