美文网首页
CrashGlobalHandler

CrashGlobalHandler

作者: Il_mondo | 来源:发表于2018-07-20 20:01 被阅读6次
public class CrashGlobalHandler implements Thread.UncaughtExceptionHandler {

    private static CrashGlobalHandler INSTANCE = new CrashGlobalHandler();

    private Context mContext;
    private Thread.UncaughtExceptionHandler mDefaultHandler;


    private CrashGlobalHandler() {
    }

    public static CrashGlobalHandler getInstance() {
        return INSTANCE;
    }

    public void init(Context context) {
        mContext = context;
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread thread, final Throwable throwable) {
        try {
            printExceptionToLocal(throwable);
        } catch (Exception e){}

        //System.exit(0);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 把错误写入本地
     */
    private void printExceptionToLocal(Throwable throwable) throws IOException {
        File cacheDir = new File(mContext.getCacheDir().getPath(), "exception.txt");
        throwable.printStackTrace(new PrintWriter(new FileWriter(cacheDir)));
    }
}

相关文章

网友评论

      本文标题:CrashGlobalHandler

      本文链接:https://www.haomeiwen.com/subject/twoqmftx.html