Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
//处理未捕获异常
}
});
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
//处理未捕获异常
}
});
public class MyApplication extends Application implements UncaughtExceptionHandler {
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
//处理未捕获异常
}
}
注意:如果使用第3种方法,需要在AndroidManifest.xml中将应用程序类指定为Application类的子类:
...