CrashGlobalHandler

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)));
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 周末两天,其实就想聊聊梦想。 我在朋友圈转过一段话, 这世上大约有很多人没有体会过,做一份热爱,擅长,出产价值的工...
    ipirate阅读 1,612评论 0 2
  • 录音:点此听语音日记 《懂了泪水,就懂了人生》 生命总是在自己的啼哭中开始,于别人的泪水里抵达终点。 医学上认为,...
    KurokoZ阅读 1,706评论 0 1
  • 一次家宴,一场雨。 没有先后,几乎是同时发生。 前一周左右,二姐就邀约我赴宴,并出任本次家宴的主厨。 我爽快答应,...
    俏村姑阅读 1,666评论 0 1
  • 该不该看伴侣的手机,这个话题对于很多情侣来说都比较敏感。既想尊重对方又有那么一些好奇心。而我认为需要保持对彼此的尊...
    Emily小太阳阅读 2,939评论 0 0

友情链接更多精彩内容