Android - Override "Sorry, the application has stopped" Dialogue with DefaultUncaughtExceptionHandler

I want the app to automatically restart when it crashes. Currently I have achieved this with the DefaultUncaughtExceptionHandler, but sometimes when the application crashes, it sends the "Sorry, the application has stopped" dialog box and it does not restart. How can I avoid the dialogue and just restart the application every time it crashes?

Thank.

@Override
public void uncaughtException(Thread thread, Throwable ex) {

    try {

        Intent intent = new Intent(activity, FrontActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(
                application.getBaseContext(), 0, intent,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);

        //Following code will restart your application after 2 seconds
        AlarmManager mgr = (AlarmManager) application.getBaseContext()
                .getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
                pendingIntent);

        //This will finish your activity manually
        activity.finish();

        //This will stop your application and take out from it.
        System.exit(2);

    } catch (RuntimeException e) {
        e.printStackTrace();
    }
}

      

+3
android uncaughtexceptionhandler


source to share


No one has answered this question yet

Check out similar questions:

3606
Close / hide Android soft keyboard
3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
3288
Correct use cases for Android UserManager.isUserAGoat ()?
2735
Stop EditText from getting focus when Activity starts
2609
Is there a unique identifier for an Android device?
2510
How to persist android activity state by persisting instance state?
2097
Is there a way to run Python on Android?
1844
What is "Context" on Android?
1002
How do I display an alert dialog on Android?
0
I'm trying to set an alarm at a specific time using the alarm manager but the alarm is triggered instantly?



All Articles
Loading...
X
Show
Funny
Dev
Pics