/*
* Copyright (c) 2014,KJFrameForAndroid Open Source Project,张涛.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kymjs.blog;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import org.kymjs.kjframe.utils.FileUtils;
import org.kymjs.kjframe.utils.SystemTool;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
/**
* UncaughtExceptionHandler:线程未捕获异常控制器是用来处理未捕获异常的。 如果程序出现了未捕获异常默认情况下则会出现强行关闭对话框
* 实现该接口并注册为程序中的默认未捕获异常处理 这样当未捕获异常发生时,就可以做些异常处理操作 例如:收集异常信息,发送错误报告 等。
* UncaughtException处理类,当程序发生Uncaught异常的时候,由该类来接管程序,并记录发送错误报告. <br>
*
* <b>创建时间</b> 2014-7-2
*
* @author kymjs (https://www.kymjs.com/)
*/
public class CrashHandler implements UncaughtExceptionHandler {
private final Context mContext;
// log文件的后缀名
public static final String FILE_NAME_SUFFIX = "KJBlog.log";
private static CrashHandler sInstance = null;
private CrashHandler(Context cxt) {
// 将当前实例设为系统默认的异常处理器
Thread.setDefaultUncaughtExceptionHandler(this);
// 获取Context,方便内部使用
mContext = cxt;
}
public synchronized static CrashHandler create(Context cxt) {
if (sInstance == null) {
sInstance = new CrashHandler(cxt);
}
return sInstance;
}
/**
* 这个是最关键的函数,当程序中有未被捕获的异常,系统将会自动调用#uncaughtException方法
* thread为出现未捕获异常的线程,ex为未捕获的异常,有了这个ex,我们就可以得到异常信息。
*/
@Override
public void uncaughtException(Thread thread, final Throwable ex) {
// 导出异常信息到SD卡中
try {
saveToSDCard(ex);
} catch (Exception e) {
} finally {
// ex.printStackTrace();// 调试时打印日志信息
System.exit(0);
}
}
// public static void sendAppCrashReport(final Context context) {
// ViewInject.create().getExitDialog(context,
// "对不起,小屁孩发脾气了,我们会替你好好教训一下他的。", new OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// System.exit(-1);
// }
// });
// }
private void saveToSDCard(Throwable ex) throws Exception {
File file = FileUtils.getSaveFile(AppConfig.saveFolder,
FILE_NAME_SUFFIX);
boolean append = false;
if (System.currentTimeMillis() - file.lastModified() > 5000) {
append = true;
}
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(
file, append)));
// 导出发生异常的时间
pw.println(SystemTool.getDataTime("yyyy-MM-dd-HH-mm-ss"));
// 导出手机信息
dumpPhoneInfo(pw);
pw.println();
// 导出异常的调用栈信息
ex.printStackTrace(pw);
pw.println();
pw.close();
}
private void dumpPhoneInfo(PrintWriter pw) throws NameNotFoundException {
// 应用的版本名称和版本号
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),
PackageManager.GET_ACTIVITIES);
pw.print("App Version: ");
pw.print(pi.versionName);
pw.print('_');
pw.println(pi.versionCode);
pw.println();
// android版本号
pw.print("OS Version: ");
pw.print(Build.VERSION.RELEASE);
pw.print("_");
pw.println(Build.VERSION.SDK_INT);
pw.println();
// 手机制造商
pw.print("Vendor: ");
pw.println(Build.MANUFACTURER);
pw.println();
// 手机型号
pw.print("Model: ");
pw.println(Build.MODEL);
pw.println();
// cpu架构
pw.print("CPU ABI: ");
pw.println(Build.CPU_ABI);
pw.println();
}
}