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

后端 未结 2 812
渐次进展
渐次进展 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"
    

提交回复
热议问题