34

I found this Android: How to auto-restart application after it's been "force closed"?

but I don't know where and how to put the alarm manager

thanks

Community
  • 1
  • 1
Mike Bryant
  • 2,455
  • 9
  • 39
  • 60

2 Answers2

72

You can catch all uncaught exceptions in your Application extension class. In the exception handler do something about exception and try to set up AlarmManager to restart your app. Here is example how I do it in my app, but I only log exception to a db.

public class MyApplication extends Application {
    // uncaught exception handler variable
    private UncaughtExceptionHandler defaultUEH;

    // handler listener
    private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler =
        new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {

                // here I do logging of exception to a db
                PendingIntent myActivity = PendingIntent.getActivity(getContext(),
                    192837, new Intent(getContext(), MyActivity.class),
                    PendingIntent.FLAG_ONE_SHOT);

                AlarmManager alarmManager;
                alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                    15000, myActivity );
                System.exit(2);

                // re-throw critical exception further to the os (important)
                defaultUEH.uncaughtException(thread, ex);
            }
        };

    public MyApplication() {
        defaultUEH = Thread.getDefaultUncaughtExceptionHandler();

        // setup handler for uncaught exception 
        Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
    }
}
Xavi
  • 20,111
  • 14
  • 72
  • 63
Maxim
  • 4,152
  • 8
  • 50
  • 77
  • I put the alarm manager in the @override but my app doesn't restart when I end the process through a task manager :/ – Mike Bryant Jan 20 '12 at 15:31
  • 1
    I doubt that, when the process is *killed*, an exception will be thrown - or any other Java code executed, for that matter. – JimmyB Jan 20 '12 at 16:04
  • 1
    I do divide on 0 to invoke exception. Catch it in my handler and after that I able to restart application. If you kill the process I agree with @HannoBinder, exception won't be thrown, otherwise when phone is running out of memorry and OS starts to kill processes we will get many exceptions thrown. – Maxim Jan 20 '12 at 16:17
  • 1
    You say it is important to re-throw critical exception. But it will not reach that code because of System.exit(2). Also, what is the point of re-throwing the exact same exception? – Dante Dec 06 '13 at 23:11
  • 1. System.exit() doesn't quit app right after execution it runs "shutdown hooks" to release resources. 2. When critical exception happened it is necessary to let the OS handle it. Can't give you detailed answer why. – Maxim Dec 09 '13 at 17:26
  • 1
    As far as I see, in spite of shutdown hooks, the code in the method coming after System.exit(2) will never be executed. Anyway, why do you need the System.exit(2)? – Jörg Eisfeld Apr 22 '15 at 23:33
2

Basically, you have to implement your own instance of an UncaughtExceptionHandler, then you will have to make sure that for every thread your App runs you call setUncaughtExceptionHandler.

Then, when an uncaught exception occurs in any of those threads, your own UncaughtExceptionHandler will be called and you can from there schedule your App's restart or whatever before passing on the exception.

I don't know if it really makes sense to just restart the App in that case, though. The user may be quite "surprised" if, in the middle of his interaction, the App 'resets' and does not resume where it was just a second ago, possibly even losing the user's previous input, etc..

Edit:

See here, the answer of Gyuri. Apart from that you only need to implement an interface, namely UncaughtExceptionHandler, and 'paste' Gyuri's code into that.

Edit #2:

For reference: A service started "sticky" might achieve the desired result, too.

Community
  • 1
  • 1
JimmyB
  • 12,101
  • 2
  • 28
  • 44