Is it possible to create some sort of global exception handler in Android?

后端 未结 2 734
渐次进展
渐次进展 2021-01-15 02:07

My application includes a series of Activities, through which the user must proceed in a linear fashion. Let\'s say that this series of activities looks like this: A (repres

相关标签:
2条回答
  • 2021-01-15 02:21

    Extend Application class

    import android.app.Application;
    import android.util.Log;
    
    public class MyApplication extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
    
                @Override
                public void uncaughtException(Thread thread, Throwable ex) {
                    Log.e("MyApplication", ex.getMessage());            
    
                }
            });
        }
    }
    

    Add the following line in AndroidManifest.xml file as Application attribute

    android:name=".MyApplication"
    
    0 讨论(0)
  • 2021-01-15 02:25
        Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler()... );
    
    0 讨论(0)
提交回复
热议问题