dev90
dev90

Reputation: 7539

How to save stacktrace before application crash

I need to store and send crash log to server. I am able to save crash log in preference before it exists, but the problem is that it restarts the application and does not display the default crash message to user.

I want the crash flow for user to be normal.

public void registerCrash(){

    Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
    {
        @Override
        public void uncaughtException (Thread thread, Throwable e)
        {
            handleUncaughtException (thread, e);
        }
    });
}


public void handleUncaughtException (Thread thread, Throwable e)
{
    thread.getStackTrace();
    saveDataToFile(e.toString());
    System.exit(-1);
}

If i change the System.exits(0), it abruptly crashes, and does not save any log in file, and with System.exit(-1);, it restarts the application on crash.

Edit

saveDateToFile()

SharedPreferences sharedPreferences= context.getSharedPreferences(CRASH_LOG, Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
prefsEditor.putString(STACK_TRACE, data);
prefsEditor.apply();

Upvotes: 0

Views: 1039

Answers (2)

ligi
ligi

Reputation: 39539

Please have a look at https://github.com/ligi/tracedroid - it can collect the crash-logs for you and you can write your own sending module

Upvotes: 0

xbadal
xbadal

Reputation: 1375

use Fabric to get crash report and many more like Installs or event etc.

it will automatically sent the crash reportto the server and let you know via email.

easy to implement, just need to include sdk here

Upvotes: 1

Related Questions