Getting the crash log and send it as email

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

    I haven't tried it but looking at it gives the current log and not the crash report refer to How do I obtain crash-data from my Android application?

    0 讨论(0)
  • 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.

    <application android:icon="@mipmap/ic_launcher" android:name=".CrashDebugApplication">
    
    0 讨论(0)
  • 2021-02-06 13:37

    Have a look at this projekt. LINK It is a small projekt to post the stacktrace to your server, so you have them on your own server.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 13:48

    I recommend you use ARCA https://github.com/ACRA/acra.

    Include arca in your build.gradle--it uses the apache 2.0 license as of 10/29.

    compile 'ch.acra:acra:4.9.0' //TODO:  Apache 2.0 license https://github.com/ACRA/acra
    

    In your class that extends Application, drop this on top of the "class" declaration.

    @ReportsCrashes(mailTo = "someone@somewhere.com",
            customReportContent = { ReportField.APP_VERSION_CODE, ReportField.APP_VERSION_NAME, ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL, ReportField.CUSTOM_DATA, ReportField.STACK_TRACE, ReportField.LOGCAT },
            mode = ReportingInteractionMode.TOAST,
            resToastText = R.string.resToastText) //you get to define resToastText
    public class MyApplication extends Application {
    

    Then override the following method from the same Application class like so:

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    
        // The following line triggers the initialization of ACRA
        ACRA.init(this);
    }
    
    0 讨论(0)
提交回复
热议问题