Catch the global exception implementation code in Android
- 2020-06-19 11:41:12
- OfStack
1. Implement UncaughtExceptionHandler and handle uncaught exceptions in method uncaughtException.
public class GlobalException implements UncaughtExceptionHandler
{
private final static GlobalException myCrashHandler = new GlobalException();
private GlobalException()
{
}
public static synchronized GlobalException getInstance()
{
return myCrashHandler;
}
public void uncaughtException(Thread arg0, Throwable arg1)
{
Trace.Log("-------------caught Exception--");
}
}
2. Inherit Application and call Thread to catch exceptions
Code:
public class MyApplication extends Application
{
public void onCreate()
{
super.onCreate();
GlobalException handler = GlobalException.getInstance();
Thread.setDefaultUncaughtExceptionHandler(handler);
}
}