Thread.getDefaultUncaughtExceptionHandler()
可以获取系统默认的UncaughtException处理器
通过Thread.setDefaultUncaughtExceptionHandler,设置我们自己的Crash处理器
实现UncaughtExceptionHandler 接口,重写uncaughtException()方法
/**
* 当UncaughtException发生时会转入该函数来处理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
// 如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(700);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}
Appliction application=Appliction.getInstance();
Intent intent = new Intent(application, shouyeActivity.class);
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秒钟后重启应用
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(mContext, mContext.getString(R.string.str_procedure_error), Toast.LENGTH_LONG) .show();
Looper.loop();
}
}.start();
// 保存日志文件
saveCrashInfo2File(ex);
return true;
}
在Appliction的onCreate()方法中
设置我们自己的Crash处理器
/**
* @author: JinPeng
* @Time:2016-12-14: 下午12:56:57
* 退出应用。关闭所有activity
*/
public void finishActivity(){
for (Activity activity : list) {
if (null != activity) {
activity.finish();
}
}
//杀死该应用进程
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
可以获取系统默认的UncaughtException处理器
通过Thread.setDefaultUncaughtExceptionHandler,设置我们自己的Crash处理器
实现UncaughtExceptionHandler 接口,重写uncaughtException()方法
/**
* 当UncaughtException发生时会转入该函数来处理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
// 如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(700);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}
Appliction application=Appliction.getInstance();
Intent intent = new Intent(application, shouyeActivity.class);
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秒钟后重启应用
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(mContext, mContext.getString(R.string.str_procedure_error), Toast.LENGTH_LONG) .show();
Looper.loop();
}
}.start();
// 保存日志文件
saveCrashInfo2File(ex);
return true;
}
在Appliction的onCreate()方法中
设置我们自己的Crash处理器
/**
* @author: JinPeng
* @Time:2016-12-14: 下午12:56:57
* 退出应用。关闭所有activity
*/
public void finishActivity(){
for (Activity activity : list) {
if (null != activity) {
activity.finish();
}
}
//杀死该应用进程
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}