package com.android.alex.exceptiondemo;
import android.app.Application;
import android.os.Looper;
import android.widget.Toast;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
/**
* @author Alex
* @date 2019/1/11.
* GitHub:https://github.com/wangshuaialex
*/
public class App extends Application implements Thread.UncaughtExceptionHandler {
@Override
public void onCreate() {
super.onCreate();
//进行异常捕获
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
//进行日志打印
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
throwable.printStackTrace(printWriter);
new Thread(){
@Override
public void run() {
super.run();
Looper.prepare();
//如果发生崩溃Toast显示信息
Toast.makeText(App.this, "程序员已祭天,请稍候再试", Toast.LENGTH_SHORT).show();
Looper.loop();
}
}.start();
}
}
全局异常捕获
最新推荐文章于 2024-07-19 23:53:29 发布