android CrashHandler 異常崩潰捕獲處理工具類

package com.jy.toolcar.util;

import android.content.Context;

import android.content.Intent;

import android.os.Environment;

import android.os.Process;

import com.jy.toolcar.ui.activity.LoginActivity;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.io.StringWriter;

import java.io.Writer;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

/**

* 異常崩潰處理

*

* 需在程序的入口進(jìn)行初始化

* CrashHandler.getInstance().init(getCarLockContext());

* Thread.setDefaultUncaughtExceptionHandler(CrashHandler.getInstance());

*/

public class CrashHandlerimplements Thread.UncaughtExceptionHandler {

private Thread.UncaughtExceptionHandlerdefaultHanlder;

? ? // 程序的Context對象

? ? private ContextmContext;

? ? private CrashHandler() {

}

public static CrashHandlergetInstance() {

return CrashHandlerHolder.INSTANCE;

? ? }

public void init(Context context) {

mContext = context;

? ? ? ? defaultHanlder = Thread.getDefaultUncaughtExceptionHandler();

? ? ? ? Thread.setDefaultUncaughtExceptionHandler(this);

? ? }

@Override

? ? public void uncaughtException(Thread t, Throwable e) {

if (!handleCrash(e)) {

defaultHanlder.uncaughtException(t, e);

? ? ? ? }else {

//TODO:僅測試時開放

? ? ? ? ? ? defaultHanlder.uncaughtException(t, e);

? ? ? ? ? ? try {

Thread.sleep(1000);

? ? ? ? ? ? }catch (Exception e1) {

e1.printStackTrace();

? ? ? ? ? ? }

Process.killProcess(Process.myPid());

? ? ? ? ? ? System.exit(1);

? ? ? ? }

}

private boolean handleCrash(Throwable e) {

if (null == e)return false;

? ? ? ? StringBuilder builder =new StringBuilder();

? ? ? ? Writer writer =new StringWriter();

? ? ? ? PrintWriter printWriter =new PrintWriter(writer);

? ? ? ? e.printStackTrace(printWriter);

? ? ? ? Throwable cause = e.getCause();

? ? ? ? while (null != cause) {

cause.printStackTrace(printWriter);

? ? ? ? ? ? cause = cause.getCause();

? ? ? ? }

printWriter.close();

? ? ? ? String msg = writer.toString();

? ? ? ? String time =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date());

? ? ? ? builder.append(time)

.append("#")

.append(msg);

? ? ? ? saveErrorFile(builder.toString());

? ? ? ? Intent intent =new Intent(mContext, LoginActivity.class);

? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

? ? ? ? mContext.startActivity(intent);

return true;

? ? }

private void saveErrorFile(String msg) {

String logFilePath =getCrashLogPath();

? ? ? ? File file =new File(logFilePath);

? ? ? ? if (!file.exists()) {

FileUtils.createFile(logFilePath);

? ? ? ? }else {

if (file.length() >=5 *1024 *1024) {//大于5M就刪除舊文件,重新生成新文件,一般不會超出大小

? ? ? ? ? ? ? ? file.delete();

? ? ? ? ? ? ? ? try {

file.createNewFile();

? ? ? ? ? ? ? ? }catch (IOException e) {

e.printStackTrace();

? ? ? ? ? ? ? ? }

}

}

BufferedWriter out =null;

? ? ? ? try {

out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));

? ? ? ? ? ? out.write(msg);

? ? ? ? }catch (Exception e) {

e.printStackTrace();

? ? ? ? }finally {

try {

if (out !=null) {

out.close();

? ? ? ? ? ? ? ? }

}catch (Exception e) {

e.printStackTrace();

? ? ? ? ? ? }

}

}

private final static class CrashHandlerHolder {

private final static CrashHandlerINSTANCE =new CrashHandler();

? ? }

public static StringgetCrashLogPath() {

StringBuilder pathBuilder =new StringBuilder();

? ? ? ? pathBuilder.append(Environment.getExternalStorageDirectory())

.append(File.separator)

.append("AppTool")

.append(File.separator)

.append("log")

.append(File.separator)

.append("error.log");

? ? ? ? return pathBuilder.toString();

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容