Getting the crash log and send it as email

后端 未结 5 640
盖世英雄少女心
盖世英雄少女心 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:34

    In my case I have a bug that i cant replicate on my phone I just want the stack trace back from a lone tester. The simplest way I could find to do this was to get it copied into the users clipboard and ask them to send it to me here is the code:

    import android.app.Application;
    import android.content.ClipData;
    import android.content.ClipboardManager;
    import android.content.Context;
    
    import java.io.PrintWriter;
    import java.io.StringWriter;
    
    /**
     * Copies the stack trace the exception that is causing your application to crash into the clip board.
     * Ask your testers to paste it into an email / text message to you.
     *
     * @author Stuart Clark
     */
    
    public class CrashDebugApplication extends Application {
      @Override
      public void onCreate() {
        super.onCreate();
    
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
          @Override
          public void uncaughtException(Thread thread, Throwable e) {
            // Get the stack trace.
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
    
            // Add it to the clip board and close the app
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("Stack trace", sw.toString());
            clipboard.setPrimaryClip(clip);
            System.exit(1);
          }
        });
    
      }
    }
    

    Then set the android:name property in Android Manifesto i.e.

    
    

提交回复
热议问题