4

I have an application and I would like to be able to restart it on the event of an error or a crash/app shutdown. I am familiar with how to register BroadcastReceivers, use alarms, etc.

Is there any signal I could intercept that the app sends out when it shuts down? Or that the OS sends out when any app shuts down?

Cristian
  • 198,401
  • 62
  • 356
  • 264
Tom G
  • 2,595
  • 5
  • 20
  • 16

1 Answers1

5

Is there any signal I could intercept that the app sends out when it shuts down? Or that the > OS sends out when any app shuts down?

No and no, AFAIK.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I was considering registering a receiver to periodically check whether my process is still running. I just couldn't figure out what action to register it on... I have one running to intercept ACTION_BOOT_COMPLETED, I was considering using ACTION_TIME_TICK but apparently you can't register that in the manifest file... – Tom G Sep 14 '10 at 20:47
  • @Tom G: "I was considering using ACTION_TIME_TICK but apparently you can't register that in the manifest file" -- correct. That would kill device performance. If the crash is for an activity, don't try to restart it. If the user wants it back, they will restart it themselves. If the crash is for a long-running service, try to rewrite it to be alarm-driven, or use an `AlarmManager` alarm to check every few minutes to make sure it is OK. If the crash is for something already managed by `AlarmManager`, just wait until the next alarm. – CommonsWare Sep 14 '10 at 20:56
  • @CommonsWare: The crash is for a long running service. I tried what you suggested with an AlarmManager. The problem I had was in figuring out what the alarm should trigger. I had it calling method once a minute to check if the process was running, however this check stopped working as soon as I killed the app. – Tom G Sep 14 '10 at 21:08
  • @Tom G: If you "killed the app" via a task killer, that eliminates alarms in Android 2.1 and earlier. Hence, you cannot truly simulate a crash via a task killer. However, getting your service to crash is easy (e.g., divide by zero), so you just need to work out a way to trigger a test crash. – CommonsWare Sep 14 '10 at 21:19
  • OK @CommonsWare, I took your advice and indeed my Alarm works after the app crashes. However, I use this: List runningApps = AM.getRunningAppProcesses(); to determine whether my app is running and the ActivityManager says that my app is still running. How does I tell whether an app has crashed? – Tom G Sep 14 '10 at 23:42
  • This is very strange...my app crashes, I click "force close", and then it closes the app. When my alarm triggers the code that checks for running processes, not only is my process still seen as "running", but it has an importance of 100, which means "this process is running the foreground UI". How is this possible??? – Tom G Sep 15 '10 at 00:59
  • @Tom G: Just because the service crashed does not mean the process crashed. The process will keep running any of your other components, or it will be recycled if all your components are destroyed. You need to implement some other test (e.g., update a heartbeat.log file every minute, and your test is "has the file been updated since my last alarm?"). – CommonsWare Sep 15 '10 at 01:04
  • OK, i'm using the logfile method and it seems to be working. Thanks for all the help @CommonsWare – Tom G Sep 15 '10 at 19:43