先贴工具类,此类可以保存手机报错设备信息,传到服务器等操作(自定义操作)
/**
* @ClassName: UnCeHandler
* @Description:程序异常退出工具类
* @Author: dingchao
* @Date: 2019/4/2 13:46
*/
public class UnCeHandler implements Thread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler mDefaultHandler;
public static final String TAG = "CatchExcep";
BaseApplication application;
public UnCeHandler(BaseApplication application){
//获取系统默认的UncaughtException处理器
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
this.application = application;
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if(!handleException(ex) && mDefaultHandler != null){
//如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
}else{
try{
Thread.sleep(2000);
}catch (InterruptedException e){
Log.e(TAG, "error : ", e);
}
Intent intent = new Intent(application.getApplicationContext(), MainActivity.class);
@SuppressLint("WrongConstant") PendingIntent restartIntent = PendingIntent.getActivity(
application.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
//退出程序
AlarmManager mgr = (AlarmManager)application.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
restartIntent); // 1秒钟后重启应用
Tool.exitApp();
// application.finishActivity();
}
}
/**
* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
*
* @param ex
* @return true:如果处理了该异常信息;否则没有处理返回false.
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
return false;
}
//使用Toast来显示异常信息
new Thread(){
@Override
public void run() {
Looper.prepare();
Toast.makeText(application.getApplicationContext(), "很抱歉,程序出现异常,即将退出.",Toast.LENGTH_SHORT).show();
// Tool.exitApp();
Looper.loop();
}
}.start();
return true;
}
}
使用方法在BaseApplication中的onCreate中
//程序异常退出重启
UnCeHandler catchExcep = new UnCeHandler(this);
Thread.setDefaultUncaughtExceptionHandler(catchExcep);
网友评论