Created
December 4, 2019 07:18
-
-
Save piyush-malaviya/ecd92eac12ed00c17c26d4b675a0c464 to your computer and use it in GitHub Desktop.
Global Exception Handling on android
This file contains hidden or 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