Getting the crash log and send it as email

后端 未结 5 576
盖世英雄少女心
盖世英雄少女心 2021-02-06 13:23

From my search, i got the below code for getting the Crash Log .

try {
      Process process = Runtime.getRuntime().exec(\"logcat -d\");
      BufferedReader buf         


        
5条回答
  •  悲哀的现实
    2021-02-06 13:47

    The best way to handle crash logs is creating an UncaughtExceptionHandler and handling it as per your requirement. Create a BaseActivity class and extend all the Activities with that and put this code stuff in the BaseActivity class.

    private Thread.UncaughtExceptionHandler handleAppCrash = 
                                             new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                Log.e("error", ex.toString());
                //send email here
            }
        };
    

    Then just enable is inside onCreate() method of your BaseActivity by using

    Thread.setDefaultUncaughtExceptionHandler(handleAppCrash);

    So, now whenever there will be a crash in your Application uncaughtException() will be called and you will have to handle the crash accordingly.

提交回复
热议问题