Forked from piyush-malaviya/Global Exception Handling on android
Created
July 7, 2021 14:52
-
-
Save TechNov/a71a16afeadced3735b2f412fbe2da1f to your computer and use it in GitHub Desktop.
Global Exception Handling on android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Stackoverflow Link: https://stackoverflow.com/a/19968400 | |
Handle uncaughtException, start activity: | |
public class MyApplication extends Application | |
{ | |
public void onCreate () | |
{ | |
// Setup handler for uncaught exceptions. | |
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler() | |
{ | |
@Override | |
public void uncaughtException (Thread thread, Throwable e) | |
{ | |
handleUncaughtException (thread, e); | |
} | |
}); | |
} | |
public void handleUncaughtException (Thread thread, Throwable e) | |
{ | |
e.printStackTrace(); // not all Android versions will print the stack trace automatically | |
Intent intent = new Intent (); | |
intent.setAction ("com.mydomain.SEND_LOG"); // see step 5. | |
intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); // required when starting from Application | |
startActivity (intent); | |
System.exit(1); // kill off the crashed app | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment