6

My application has one activity which starts two services but does not bind them. If I select return button to exit application (I cannot see it in task manager), both of the services started by application keep running. However, if I goto task manager and kill application, both of the services are stopped. I am not sure if it is intended behaviour but I want the services to keep running even after application exits. Any thoughts please.

Thanks

Androidme
  • 3,145
  • 3
  • 24
  • 27

4 Answers4

9

That is the intended behavior of Task Managers (and force stop in ManageApplication). What good would stopping an application do if it left running the background work that the application was doing?

There is no way for you to prevent the user from killing your service on a stock version of Android OS

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Thanks Tim. I need to find another way to start my service – Androidme Jul 06 '12 at 14:47
  • 3
    @bsengar No, actually you need to respect the fact that the user wanted to stop your application. Any attempt the circumvent the users will to stop your application is considered malicious. It is after all their device, not yours. – FoamyGuy Jul 06 '12 at 14:54
  • @bsengar And as we said, regardless how HOW you start the service, it will die when the application is killed. I recommend just creating a really good save state algorithm and letting it be. – ahodder Jul 06 '12 at 14:57
  • 3
    "It is after all their device, not yours" - Well, its actually my company's device which is given to them so that the organisation can make sure the application is running all the time. They have contract with the company and are aware of this application running. The problem however is, they kill the application and then tell us that the application died by itself. Well...unfortunately Android doesn't support this kind of features – Androidme Jul 06 '12 at 15:10
  • @bsengar "unfortunately Android doesn't support this kind of features" -- The stock builds of the OS that are released on devices sold to the general public do not support it because it is malicious. However the beautiful thing about Android is you are free to checkout your own copy of the OS source and make the modifications necessary to allow for this behavior. Which is what you'll have to do if you want to make your implementation truly enforceable. – FoamyGuy Jul 06 '12 at 15:40
  • How do you know the users are killing the application? – David Wasser Jul 07 '12 at 11:15
  • @Tim I was actually referring to OP's comment that "they kill the application and then tell us the application died". I'm wondering if he can really be sure that is happening, or if he just assumes that is happening. I've no issue with your answer, I was just getting in on the discussion in comments. – David Wasser Jul 07 '12 at 19:23
  • @DavidWasser ahh, I see. I cannot speak on his behalf, but one possibility is that they have ACRA or some other crash reporting that would tell them if they application had died, but not if it had been manually stopped. Pure speculation on my part though. I'd be interested to know his response too. – FoamyGuy Jul 07 '12 at 19:25
2

This is the behaviour expected. Services do not run in their own process. When you application is killed, your entire process dies with it.

In the documentation I attached, there is an orange block a page down (unfortunately, I don't think I can link to it :-( ) That will tell you pretty much what a service is in a nutshell.

ahodder
  • 11,353
  • 14
  • 71
  • 114
0

I am not sure! But you should give a try to this

Community
  • 1
  • 1
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
0

As it was already mentioned, it's expected behavior. Killing an app, which started the Service should kill running Service as well. If you want to keep your Service running when app is killed, you should start the Service in a separate process, what can be achieved by the following declaration in the Manifest:

<service
    android:name=".ServiceClassName"
    android:process=":yourappname_background" >
    ...

Reference: https://stackoverflow.com/a/15474347/1150795

Please note that usually app is not killed until a user explicitly kills it from the task/app manager or system kills it due to limited resources, low battery or similar reason. I'm not sure if it's typical use case to handle such things and if you really need to care about that.

Community
  • 1
  • 1
Piotr Wittchen
  • 3,853
  • 4
  • 26
  • 39