how to implement uncaughtException android

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

  • ADT will not allow creation of Android Activity
  • Android soft keyboard resize layout
  • Use microphone in multiple app simultaneously in Android
  • Open Chromecast YouTube video from my Android app
  • how to solve Unable to find explicit activity in firebase AuthUi?
  • Clone textview to append it to a ViewGroup
  • thanks

    Related posts:

    Android onPreviewFrame is not called
    Android Studio 2.3 doesn't have Test Instrumentation specifier in UI
    Is api key needed in SafetyNet attestation?
    Android, Tabs without Actionbar
    how to use visible and invisible for a button in android
    Multiple rectangles not drawing on canvas
  • Google Play Developer change home currency
  • Navigation Drawer highlight selected item not working
  • Edittext change border color with shape.xml
  • Firebase Authentication FirebaseNetworkException: A network error (such as timeout, interrupted connection or unreachable host) has occurred
  • Webcal / .ics subscribe to feed from within Android App
  • Use a handheld bluetooth printer with android
  • 2 Solutions collect form web for “how to implement uncaughtException android”

    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);
        }
    }
    

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.