62, when the app encountered a global exception to avoid direct exit, how to let the app take over exception handling?

Source: Internet
Author: User
Tags dateformat

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.