版權全部:@boliu
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.util.Properties;
import org.json.JSONObject;
import com.yuexue.tool.L;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Process;
/**
* @brief 全局異常處理
*/
public class CrashHandler implements UncaughtExceptionHandler {
/**
* @brief Debug Log tag
* */
public static final String TAG = "CrashHandler";
/**
* @brief 系統默認的UncaughtException處理類
* */
private Thread.UncaughtExceptionHandler mDefaultHandler;
/**
* @brief CrashHandler實例
* */
private static CrashHandler INSTANCE;
/**
* @brief 程序的Context對象
* */
private Context mContext;
/**
* @brief 使用Properties來保存設備的信息和錯誤堆棧信息
* */
private Properties mDeviceCrashInfo = new Properties();
private static final String VERSION_NAME = "versionName";
private static final String VERSION_CODE = "versionCode";
private static final String STACK_TRACE = "STACK_TRACE";
/**
* @brief保證只有一個CrashHandler實例
* */
private CrashHandler() {
}
/**
* @brief 獲取CrashHandler實例 ,單例模式
* */
public static CrashHandler getInstance() {
if (INSTANCE == null) {
INSTANCE = new CrashHandler();
}
return INSTANCE;
}
/**
* @brief 初始化,註冊Context對象, 獲取系統默認的UncaughtException處理器,
* 設置該CrashHandler爲程序的默認處理器
*
* @param ctx
*/
public void init(Context ctx) {
mContext = ctx;
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
* @brief 當UncaughtException發生時會轉入該函數來處理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
// 若是用戶沒有處理則讓系統默認的異常處理器來處理
mDefaultHandler.uncaughtException(thread, ex);
} else {
// Sleep一會後結束程序,此處能夠測試一下決定時間,由於不一樣sdk機制不一樣
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
L.e("uncaughtException", e.getMessage());
}
Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
}
/**
* @brief 自定義錯誤處理,收集錯誤信息 發送錯誤報告等操做均在此完成. 開發者能夠根據本身的狀況來自定義異常處理邏輯
*
* @param ex
* @return true:若是處理了該異常信息;不然返回false
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
return true;
}
mDeviceCrashInfo.put(STACK_TRACE, ex.getMessage());
// 收集設備信息
collectCrashDeviceInfo(mContext);
JSONObject jsonObject = new JSONObject(mDeviceCrashInfo);
L.d("DeviceCrashInfo", jsonObject.toString());
// 走友盟統計,暫時省略了其餘處理
//其餘可增長處理,如存儲成文件提交到本身服務端,錯誤日誌打包,下次提交,均可以在下面增長處理機制
// MobclickAgent.reportError(mContext,jsonObject.toString());
return true;
}
/**
* @brief 收集程序崩潰的設備信息
*
* @param ctx
*/
public void collectCrashDeviceInfo(Context ctx) {
try {
PackageManager pm = ctx.getPackageManager();
PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(),
PackageManager.GET_ACTIVITIES);
if (null != pi) {
mDeviceCrashInfo.put(VERSION_NAME,
pi.versionName == null ? "not set" : pi.versionName);
mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode + "");
}
} catch (NameNotFoundException e) {
L.e(TAG, "Error while collect package info: " + e.getMessage());
}
// 使用反射來收集設備信息.在Build類中包含各類設備信息,
// 例如: 系統版本號,設備生產商 等幫助調試程序的有用信息
Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
mDeviceCrashInfo.put(field.getName(), field.get(null)
.toString());
} catch (Exception e) {
L.e(TAG, "Error while collect crash info: " + e.getMessage());
}
}
}
}