ANDROID_ Program unhandled exception capture and processing

Source: Internet
Author: User
Tags throwable

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

Related Article

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.