android global exception capture and send exception to mailbox

public class AppException extends Exception implements UncaughtExceptionHandler {

private static final String TAG = "AppException";
private static final long serialVersionUID = -6262909398048670705L;
private String message;
private Thread.UncaughtExceptionHandler mDefaultHandler;
private AlertDialog appCrashDialog;
private Activity activity;

private AppException() {
super();
this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
}

public AppException(String message, Exception excp) {
super(message, excp);
this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

/**
* 获取APP异常崩溃处理对象
*
* @param context
* @return
*/
public static AppException getAppExceptionHandler() {
return new AppException();
}

@Override
public void uncaughtException(Thread thread, Throwable ex) {

if (!handleException(ex) && mDefaultHandler != null) {
mDefaultHandler.uncaughtException(thread, ex);
}

}

/**
* 自定义异常处理
*
* @param ex
* @return true: The exception information is processed; otherwise, return false
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
return false;
}

activity = AppManager.getAppManager().currentActivity();
Log. v(TAG, "crash activity is : " + activity);
if (activity == null) {
return false;
}
final String fileName = saveErrorToFile(ex);
new Thread() {
@Override
public void run() {
Looper. prepare();
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Warm reminder");
builder.setMessage("The program crashed!");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
activity.finish();
//UIHelper.Exit(activity);
}
});
appCrashDialog = builder .create();
appCrashDialog.setCanceledOnTouchOutside(false);
appCrashDialog.show();
Looper.loop();
}
}.start();

return true;
}

// used to store device information and exception information
private Map<String, String> infos = new HashMap<String, String>();
// used to format the date as part of the log file name
private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

/**
* Save the error message to a file
* @param ex
* @return 返回文件名称,便于将文件传送到服务器
*/
private String saveErrorToFile(Throwable ex) {
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, String> entry : infos.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 {
long timestamp = System.currentTimeMillis();
String time = formatter.format(new Date());
String fileName = "error-" + time + "-" + timestamp + ".log";
String file_dir = Utils.getSDPath();
File dir = new File(file_dir + "/log/");
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir.toString() + "/" + fileName);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
//sendErrorToServer(sb.toString());
if(!sb.toString().contains("AppException$1.run")){
sendMail(sb.toString());
Log.e("123","ErrorToFile = "+ sb.toString());
}
fos.close();
return file.toString();
} catch (Exception e) {
}
return null;
}


private void sendMail(final String string) {
new Thread(){
public void run() {
MailUtils mail = new MailUtils();
mail.send("[email protected]", "ikuaishouApp崩溃日志("+StringUtils.formatTime(System.currentTimeMillis())+")", string);
};
}.start();
}
}

然后在自己的application中的oncreate方法调用:
Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler());

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326768790&siteId=291194637