zundi
zundi

Reputation: 2457

Creating a custom crash reporter

I'm looking to report my app crashes to a custom backend (instead of using an existing crash reporter such as Firebase).

How can I catch fatal crashes in Android?

Thanks!

Upvotes: 0

Views: 683

Answers (2)

Aung Si Min Htet
Aung Si Min Htet

Reputation: 1214

First you need to write an Exception Handler like this.

public class ExceptionHandler implements Thread.UncaughtExceptionHandler {

    @SuppressWarnings("deprecation")
    public void uncaughtException(Thread thread, Throwable exception) {

        // Do your code here
        Log.i("Localized_Message", exception.getLocalizedMessage());

    }
}

Then use it by calling this class from your activity.

Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler((Activity) MainActivity.this, MainActivity.class)); // Change Main Activity with your Activity

Upvotes: 1

civic.LiLister
civic.LiLister

Reputation: 2197

Have a try to write a class that implements interface java.lang.Thread.UncaughtExceptionHandler. In your implementation method public void uncaughtException(Thread thread, Throwable ex), the exception object is "ex" and you could do what you want there.

Upvotes: 0

Related Questions