如何在不影响正常碰撞流程的情况下捕捉碰撞 [英] How to catch crash without disturbing the normal crash flow

查看:108
本文介绍了如何在不影响正常碰撞流程的情况下捕捉碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有很好的工具,例如 crashlytics ,但是我有一项要求,我无法使用这些工具。

I know there are already good tools available like crashlytics, but i have a requirment and i can't use these tools.

我需要做的就是在应用程序存在之前保存崩溃日志,并且在不干扰正常流程的情况下,并使用任何第三方库

All i need to is to save the crash log before application exists, and that without disturbing the normal flow, and using any 3rd party library

我已经实现了它,但是它干扰了Android崩溃的正常流程。我想保存日志,但不想打扰正常的崩溃流程。

I have implemented this, but it has disturb the normal flow of android crashing. I want to save the log, but don't want to disturb the normal crashing flow.

这是我要保存崩溃日志的方式。

This is what i am doing to save the crash log.

    public void registerCrash(){

        Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
        {
            @Override
            public void uncaughtException (Thread thread, Throwable e)
            {
                handleUncaughtException (thread, e);
            }
        });
    } 


    public void handleUncaughtException (Thread thread, Throwable e)
    {
        thread.getStackTrace();
        savePreferenceData(e.toString());
        System.exit(0);
    }


    public void savePreferenceData(String data) {
        SharedPreferences sharedPreferences= context.getSharedPreferences(LOG_DATA, Context.MODE_PRIVATE);
        SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
        prefsEditor.putString(STACK_TRACE, data);
        prefsEditor.apply();
}

问题是,它重新启动了应用程序,而没有给出默认的Alert对话框给用户。 不幸的是,应用程序已停止。

The problem is that, It restarts the application, without giving the default Alert dialog to user. Unfortunately App has stopped.

请指导我如何保存崩溃日志,而不会干扰正常的崩溃机制

Kindly guide me how to save crash log witout disturbing the normal crash mechanisim

推荐答案

在设置UncaughtExceptionHandler之前,请跟踪之前设置的那个,并在完成后从您自己的处理程序中调用它:

Before setting the UncaughtExceptionHandler, keep track of the one that was set before and simply call it from your own handler after you are done:

private Thread.UncaughtExceptionHandler defaultExceptionHandler;

public void registerCrash(){
    defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();

    Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler(){
        @Override
        public void uncaughtException (Thread thread, Throwable e){
            handleUncaughtException (thread, e);
            if(defaultExceptionHandler != null){
                defaultExceptionHandler.uncaughtException(thread, e);
            }
        }
    });
} 

这篇关于如何在不影响正常碰撞流程的情况下捕捉碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆