Getting the crash log and send it as email

android, crash, email

Solution

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.

Problem

From my search, i got the below code for getting 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 do i add this code, so that i should get the crash report whenever my app crashes . Also i want to email it or send to the server, but after the app getting crashed, how to call the action to send email/HTTP post method . Please advise and thanks in advance .

Original source

Related problems