package bwie.com.shizhan2.view;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
public class CrashHandler implements Thread.UncaughtExceptionHandler{
public static final String TAG="CrashHandler";
private static CrashHandler mInstance;
/**
* 系统默认UncaughtExceptionHandler
*/
private Thread.UncaughtExceptionHandler mDefaultHandler;
/**
* context
*/
private Context mContext;
/**
* 获取CrashHandler实例
*/
public static synchronized CrashHandler getInstance() {
if (null == mInstance) {
mInstance = new CrashHandler();
}
return mInstance;
}
public void init(Context context) {
mContext = context;
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
//设置该CrashHandler为系统默认的
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread t, Throwable e) {
//自己处理
if (!handleException(e) && mDefaultHandler != null) {
//如果自己没处理交给系统处理
mDefaultHandler.uncaughtException(t, e);
} else {
try {//延迟3秒杀进程
Thread.sleep(2000);
} catch (InterruptedException es) {
Log.e(TAG, "error : ", es);
}
}
}
/**
* 收集错误信息.发送到服务器
*
* @return 处理了该异常返回true, 否则false
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
return false;
}
//添加自定义信息
addCustomInfo();
//使用Toast来显示异常信息
new Thread() {
@Override
public void run() {
Looper.prepare();
//在此处处理出现异常的情况
Toast.makeText(mContext, "程序开小差了呢..", Toast.LENGTH_SHORT).show();
Looper.loop();
}
}.start();
return true;
}
/**
* 添加自定义参数
*/
private void addCustomInfo() {
Log.i(TAG, "addCustomInfo: 程序出错了...");
}
}
调用application
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
CrashHandler.getInstance().init(this);
}
}