if (mInstance == null){
mInstance = new CrashHandler();
}
return mInstance;
}
/**
- 初始化
*/
public void init() {
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
- 当UncaughtException发生时会转入该函数来处理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
try {
handleException(ex);
Thread.sleep(1000);
//退出所有Activity
AppManager.getAppManager().finishAllActivity();
//友盟,用来提交错误日志
MobclickAgent.reportError(BaseApplication.getInstance(), “崩溃:” + ex.getCause());
android.os.Process.killProcess(android.os.Process.myPid()); // 获取PID
System.exit(0); // 常规java、c#的标准退出法,返回值为0代表正常退出
} catch (InterruptedException e) {
}
}
/**
-
自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
-
@param ex
-
@return true:如果处理了该异常信息;否则返回false.
*/
private boolean handleException(final Throwable ex) {
if (ex == null) {
return false;
}
new Thread() {
@Override
public void run() {
//子线程 自己管理Looper
Looper.prepare();
ToastUtils.showMessage(BaseApplication.getInstance(), “很抱歉,程序出现异常,即将退出”);
ex.printStackTrace();
Looper.loop();
}
}.start();
collectDeviceInfo();
saveCatchInfo2File(ex);
return true;
}
/**
- 收集设备参数信息
*/
private void collectDeviceInfo() {
mErrorMap.put(“versionName”, PackageUtils.getVersionName(BaseApplication.getInstance()));
mErrorMap.put(“versionCode”, PackageUtils.getVersionCode(BaseApplication.getInstance()) + “”);
//通过反射获取所有的字段信息 比如时间,设备系统版本,设备型号等等
Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
mErrorMap.put(field.getName(), field.get(null).toString());
} catch (Exception e) {
}
}
}
private String getFilePath() {
String file_dir;
boolean isSDCardExist = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
boolean isRootDirExist = Environment.getExternalStorageDirectory().exists();
if (isSDCardExist && isRootDirExist) {
file_dir = Environment.getExternalStorageDirectory().getAbsolutePath() + “/crashlog/”;
} else {
file_dir = MyApplication.getInstance().getFilesDir().getAbsolutePath() + “/crashlog/”;
}
return file_dir;
}
/**
-
保存错误信息到文件中
-
@param ex
-
@return 返回文件名称, 便于将文件传送到服务器
*/
private String saveCatchInfo2File(Throwable ex) {
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, String> entry : mErrorMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
sb.append(key + “=” + value + “\n”);
}
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
String result = writer.toString();
sb.append(result);
try {
String time = mFormatter.format(new Date());
String fileName = “crash-” + time + “.log”;
String file_dir = getFilePath();
File dir = new File(file_dir);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(file_dir + fileName);
if (!file.exists()) {
file.createNewFile();