Receive a crash log and email it

From my search, I got the code below to get the crash log.

try { Process process = Runtime.getRuntime().exec("logcat -d"); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); StringBuilder log=new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { log.append(line); } 

But where can I add this code, so I should get a crash report whenever my application crashes.

I also want to send it by e-mail or send it to the server, but after the application is broken, how to call an action to send the e-mail / HTTP mail method.

Please advise in advance and in advance.

+4
source share
5 answers

The best way to handle crash logs is to create an UncaughtExceptionHandler and handle it as per your requirement. Create the BaseActivity class and continue with all the activities with it and put this code material 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 inside the onCreate() method of your BaseActivity using

Thread.setDefaultUncaughtExceptionHandler(handleAppCrash);

So, now whenever a crash occurs in your uncaughtException() application, you will need to handle the crash accordingly.

+16
source

Take a look at this project. LINK This is a small project for publishing stacktrace to your server, so you have them on your own server.

0
source

I have not tried, but looking at it, it displays the current log, and not a failure report, refer to How do I get emergency data from my Android application?

0
source

In my case, I have an error that I cannot reproduce on my phone. I just need a stack trace from a single tester. The easiest way I could find for this is to copy it to 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 the android:name manifest

 <application android:icon="@mipmap/ic_launcher" android:name=".CrashDebugApplication"> 
0
source

I recommend using 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 the application, drop that above 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 as:

 @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); // The following line triggers the initialization of ACRA ACRA.init(this); } 
0
source

All Articles