1. Create a class for Crashhandler
1 ImportAndroid.content.Context;2 ImportAndroid.os.Looper;3 ImportAndroid.util.Log;4 ImportAndroid.widget.Toast;5 6 ImportJava.text.DateFormat;7 ImportJava.text.SimpleDateFormat;8 ImportJava.util.Locale;9 Ten /** One * author:created by Zzl on 15/11/3. A */ - Public classCrashhandlerImplementsThread.uncaughtexceptionhandler { - Private Static FinalString TAG = Crashhandler.class. Getsimplename (); the - Private StaticCrashhandler instance;//single-case mode - - PrivateContext context;//Program Context Object + PrivateThread.uncaughtexceptionhandler Defaluthandler;//system default Uncaughtexception processing class - PrivateDateFormat formatter =NewSimpleDateFormat ( +"Yyyy-mm-dd_hh-mm-ss.sss", Locale.china); A at PrivateCrashhandler () { - - } - - /** - * Get Crashhandler instances in * - * @returnCrashhandler to */ + Public StaticCrashhandler getinstance () { - if(Instance = =NULL) { the synchronized(Crashhandler.class) { * if(Instance = =NULL) { $Instance =NewCrashhandler ();Panax Notoginseng } - } the } + A returninstance; the } + - /** $ * Initialization of exception handling $ * - * @paramContext - */ the Public voidinit (Context context) { - This. Context =context;Wuyi //get the system default Uncaughtexception processor theDefaluthandler =Thread.getdefaultuncaughtexceptionhandler (); - //set the Crashhandler as the default processor for the program WuThread.setdefaultuncaughtexceptionhandler ( This); - } About $ /** - * When uncaughtexception occurs, it is transferred to the function to handle - */ - @Override A Public voiduncaughtexception (thread thread, Throwable ex) { + the //Custom error Handling - Booleanres =HandleException (ex); $ if(!res && Defaluthandler! =NULL) { the //let the system default exception handler handle if the user does not handle the defaluthandler.uncaughtexception (thread, ex); the the}Else { - Try { inThread.Sleep (3000); the}Catch(interruptedexception e) { theLOG.E (TAG, "Error:", e); About } the //Exit Program the //android.os.Process.killProcess (Android.os.Process.myPid ()); the //system.exit (1); + } - } the Bayi /** the * Custom error handling, collect error messages to send error reports and so on are done here. the * - * @paramex - * @returntrue: Returns False if the exception information is processed; the */ the Private BooleanHandleException (FinalThrowable ex) { the if(ex = =NULL) { the return false; - } the the NewThread () { the 94 @Override the Public voidrun () { the Looper.prepare (); the 98 ex.printstacktrace (); AboutString err = "[" + ex.getmessage () + "]"; -Toast.maketext (Context, "The program has an exception.")err, Toast.length_long)101 . Show ();102 103 Looper.loop ();104 } the 106 }.start ();107 108 //collecting device parameter information \ Log information109 return true; } the}
2, in the Application OnCreate () in the case of the change Crashhandler
1 @Override 2 Public void onCreate () {3 Super . OnCreate (); 4 initinformdata (); 5 Mcontext = getapplicationcontext (); 6 Crashhandler Crashhandler = crashhandler.getinstance (); 7 Crashhandler.init (mcontext); 8 9 }
This way, when the app encounters an uncaught exception, it does not flash directly, depending on how the developer handles the business needs.
62, when the app encountered a global exception to avoid direct exit, how to let the app take over exception handling?