1. Introduction to an uncaught exception thrown by a program can cause the program to exit unexpectedly, the interface is unfriendly and a critical error message should be logged to the server. The main use of Uncaughtexceptionhandler here
When we enter the activity's OnCreate function, we set up to handle the uncaught exception
1. Introduction to an uncaught exception thrown by a program can cause the program to exit unexpectedly, the interface is unfriendly and a critical error message should be logged to the server. The main use of UNCAUGHTEXCEPTIONHANDLER2. Code implementation public class Crashhandler implements Uncaughtexceptionhandler {public static Final String TAG = CopyOfCrashHandler.class.getSimpleName ();//system default Uncaughtexception processing class private Thread.uncaughtexceptionhandler mdefaulthandler;private static Copyofcrashhandler instance;private Context MContext; Private Copyofcrashhandler () {}/** gets Crashhandler instance, singleton mode */public static Copyofcrashhandler getinstance () {if (instance = = NULL) instance = new Copyofcrashhandler (); return instance;} /** * Initialize */public void init (context context) {Mcontext = context;//record the default Uncaughtexceptionhandlermdefaulthandler = Threa D.getdefaultuncaughtexceptionhandler ();//thread.setdefaultuncaughtexceptionhandler (this);} /** * When uncaughtexception occurs, it is transferred to the function to process */@Overridepublic void uncaughtexception (thread thread, Throwable ex) {if (! HandleException (thread, ex) && Mdefaulthandler! = null) {//If the user does not handle it, let the system default exception handler handle the Mdefaulthandler.uncaughteXception (thread, ex);} else {try {thread.sleep ()} catch (Interruptedexception e) {e.printstacktrace ();} Android.os.Process.killProcess (Android.os.Process.myPid ()); System.exit (1);}} /** * Custom error handling, collect error messages to send error reports, etc. are done here. * * @param ex * @return true: Returns False if the exception information is processed; */private boolean handleexception (thread thread, Throwable ex) {if (ex = = null) {return false;} StringBuffer sb = new StringBuffer () sb.append (thread + ", cause by:" + ex). Append ("\r\n\r\n"); stacktraceelement[] elements = Ex.getstacktrace (); for (int i = 0; i < elements.length; i++) {Sb.append (elements[i].tost Ring () + "\ r \ n"); Record key error message, can be stored locally and uploaded to server//logutil.bug (TAG, sb.tostring ());//Open new Activity friendly interface prompt//util.showdialog (Mcontext, "Time:" + Util.formatsimpledateandtime (New Date ()), "The program has an exception, please record the time and prompt the developer!"); return true;}}
ANDROID_ Program unhandled exception capture and processing