I want to perform action/event when application killed from task manager or any other app. Is there any to perform action when application killed. My application is running in background like service. If i terminate the application then main service stop . I want to start it again.
-
Keep in mind that in general always-on services are frowned upon due to the impact they have on the device (CPU utilization, battery life, and so on). It might help if you give more details on what your scenario is, so that people here can offer you more feasible approaches to achieving it. – Franci Penov Jan 23 '13 at 12:53
-
1"I want to start it again" -- what you want does not matter. What your *users* want matters. If your users wanted your app to be running, they would not have forcibly stopped it. If you fail to respect your users, nobody will respect you. – CommonsWare Jan 23 '13 at 13:34
3 Answers
No, there's no reliable way to know if your application was killed by a another process. The whole point of "killing" an app is to terminate it as soon as possible, without letting it run any code.
== Do not actually use the following suggestions in production application. They are here purely as potential technical solutions, but in general are not a good idea for apps running on end user devices. ==
It might be possible to use IBinder.linkToDeath()
from a secondary application, which acts as a monitor for your primary one. However, you will have to convince the user to install the secondary app as well. If you can do it, you could establish two-side monitoring between the two apps, and have one of them restart the other if the second is killed.
You could also attempt to set an alarm through the AlarmManager
that fires every so often, to restart your application if it happens to be killed. However, if your alarm period is too big, you risk having certain period of time where your app is not running. And if your time period is too small, most likely your app will not be allowed by Google in the Google Play Store, and the malware app analysis on the phone (JB+) might kick in. Also, alarms that kick in too often will keep the device awaken, and drain the battery very fast.

- 74,861
- 18
- 132
- 169
-
You declare a service in one of the applications. In the other, you call `bindService()`, to get an `IBinder` client for that service. You don't need to implement your own interface on it, as your only purpose is to detect process death. Once you get the `IBinder` in the second app, you call`bindToDeath()` with an instance of a class that implements `IBinder.DeathRecipient`. If the first app process dies, that instance's `binderDied()` will be called, which is an indication to the second app that the first app died. – Franci Penov Jan 24 '13 at 07:23
-
If you need to shut down the first app cleanly, you will have to come up with a bit more complex mechanism to notify the second app so it can release the `IBinder` it holds, or it will keep the first app alive for as long as the second app is alive. – Franci Penov Jan 24 '13 at 07:23
If you kill some process, you just kill it, so it stops working immediately. There is no event sent to the application.

- 148,279
- 62
- 259
- 315
I looked for the same thing and the answer that i found is : NO, the application does not go to OnDestroy()
or anything like that.

- 1,050
- 2
- 10
- 27