Inherit the Uncaughtexceptionhandler interface, override the method, and do the processing
Package com.example.exp;
Import Java.lang.Thread.UncaughtExceptionHandler;
Import com.example.threadtest.MainActivity;
Import Android.app.AlertDialog;
Import android.app.Application;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnClickListener;
Import android.content.Intent;
Import Android.os.Looper;
Import Android.util.Log;
public class Crashhandler implements Uncaughtexceptionhandler {
Public static final String TAG = "Crashhandler";
private static Crashhandler INSTANCE = new Crashhandler ();
private application Mcontext;
private Thread.uncaughtexceptionhandler Mdefaulthandler;
Private Crashhandler () {
}
Public static Crashhandler getinstance () {
return INSTANCE;
}
Public void init (Application ctx) {
mcontext = CTX;
Mdefaulthandler = Thread.getdefaultuncaughtexceptionhandler ();
Thread.setdefaultuncaughtexceptionhandler (this);
}
@Override
Public void Uncaughtexception (thread thread, Throwable ex) {
//if (!handleexception (ex) && Mdefaulthandler! = null) {
//Mdefaulthandler.uncaughtexception (thread, ex);
//} else {
//Android.os.Process.killProcess (Android.os.Process.myPid ());
//System.exit (ten);
// }
if (!handleexception (ex) && Mdefaulthandler! = null) {
//If the user does not handle it, let the system default exception handler handle
mdefaulthandler.uncaughtexception (thread, ex);
} else {
try {
Thread.Sleep (+);
} catch (Interruptedexception e) {
log.e (TAG, "error:", e);
}
//Restart the program, commenting the exit program above
Intent Intent = new Intent ();
Intent.setclass (mcontext,mainactivity.class);
intent.addflags (intent.flag_activity_new_task);
mcontext.startactivity (intent);
android.os.Process.killProcess (Android.os.Process.myPid ());
}
}
/**
* Custom error handling, collect error messages, send error reports, and so on. Developers can customize exception handling logic to their own situation
*
* @param ex
* @return true: Returns False if the exception information is processed;
*/
Private Boolean handleexception (Throwable ex) {
if (ex = = null) {
return true;
}
//todo
return true;
}
}
Android Exception capture processing