views:

115

answers:

2

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?

A: 

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
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
@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
@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
@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
OK @CommonsWare, I took your advice and indeed my Alarm works after the app crashes. However, I use this: List<ActivityManager.RunningAppProcessInfo> 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
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
@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
OK, i'm using the logfile method and it seems to be working. Thanks for all the help @CommonsWare
Tom G
A: 

I tried to do the same thing in FroYo because I have an app that needs to be running at all times.

To test this I created a service that spawns a Thread that crashes the App (by dividing by zero).

I have set up an Repeating Alarm that starts the App every 30 seconds if it is not running.

However, I discovered that if the AlarmManager restarts the App after the first crash and it crashes again (which it of course will). It will not be restarted a second time. Probably because FroYo consider it to be a waste of resources to restart an ever-crashing App.

Any thoughts on this? I could really use e workaround because my App needs to be running at all times or lives will be lost! ( I will use the App to cheat on the SAT, so that I may become a doc and save lives. However, I cannot restart the app during the test so it is kind of important that it does not crash!)

Cheater