Here, check for the link for reference.
In here you create a class say ExceptionHandler that implements java.lang.Thread.UncaughtExceptionHandler
..
Inside this class you will do your life saving stuff like creating stacktrace and gettin ready to upload error report etc....
Now comes the important part i.e. How to catch that exception.
Though it is very simple. Copy following line of code in your each Activity just after the call of super method in your overriden onCreate
method.
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
Your Activity may look something like this…
public class ForceClose extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
setContentView(R.layout.main);
}
}
And this is a sample ExceptionHandler class:
public class ExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final String TAG = "ExceptionHandler";
@Override
public void uncaughtException(@NonNull Thread thread, @NonNull Throwable exception) {
Log.e(TAG, "uncaughtException: " + "\n" + getErrorReport(exception));
Log.i(TAG, getDeviceInfo());
Log.i(TAG, getFirmwareInfo());
stopTheApp();
}
private String getErrorReport(@NonNull Throwable exception) {
ApplicationErrorReport.CrashInfo crashInfo = new ApplicationErrorReport.CrashInfo(exception);
return "\nCAUSE OF ERROR\n" +
crashInfo.stackTrace;
}
private String getDeviceInfo() {
return
"\nDEVICE INFORMATION\n" +
"Brand: " +
Build.BRAND +
"\n" +
"Device: " +
Build.DEVICE +
"\n" +
"Model: " +
Build.MODEL +
"\n" +
"Id: " +
Build.ID +
"\n" +
"Product: " +
Build.PRODUCT +
"\n";
}
private String getFirmwareInfo() {
return "\nFIRMWARE\n" +
"SDK: " +
Build.VERSION.SDK_INT +
"\n" +
"Release: " +
Build.VERSION.RELEASE +
"\n" +
"Incremental: " +
Build.VERSION.INCREMENTAL +
"\n";
}
private void stopTheApp() {
android.os.Process.killProcess(android.os.Process.myPid());
}
}