Best practices for Android exception handling

Source: Internet
Author: User

A good app exception handling mechanism I think it should contain at least the following features:

1. The error message can be uploaded to the server so that developers can continue to improve the app

2. The error message should at least contain information about whether the main process is in the main thread, etc. that can help the programmer to locate

3. It is best to include phone hardware and software information.

4. The exception thrown by the main process is best handled by the system itself, which allows the user to perceive the kind (and of course you can define a more interesting system dialog box for yourself, and refer to a variety of interesting 404 interfaces)

5. The exception that is thrown by the child process is best not to be perceived by the user. such as push, such as the user-perceived weak association. The best thing to do is to kill it right away. No more handing over to the system.

The code below.

1  Packagecom.example.administrator.exceptiontest;2 3 ImportAndroid.app.ActivityManager;4 Importandroid.app.Application;5 ImportAndroid.content.Context;6 7 /**8 * Created by Administrator on 2015/12/9.9  */Ten  Public classBaseapplicationextendsApplication { One  A      Public StaticContext Mcontext; -     //Default exception Handling -      Public StaticThread.uncaughtexceptionhandler Defaultuncaughtexceptionhandler; the  - @Override -      Public voidonCreate () { -         Super. OnCreate (); +Mcontext = This; -         //get the default exception handling first handler +Defaultuncaughtexceptionhandler =Thread.getdefaultuncaughtexceptionhandler (); AThread.setdefaultuncaughtexceptionhandler (NewBaseuncaughtexceptionhandler ()); at     } -  -  -}
1  Packagecom.example.administrator.exceptiontest;2 3 ImportAndroid.app.ActivityManager;4 ImportAndroid.content.Context;5 ImportAndroid.os.Looper;6 7 /**8 * Created by Administrator on 2015/12/9.9  */Ten  Public classUtils { One  A     /** - * Determines whether the main thread is executed if TRUE is not returned false -      */ the      Public Static BooleanIsinmainthread () { -         //Watch out for this place, we can't be sure we can get the value of mylooper. For example, your thread is not bound to a message loop -         //then your mylooper return must be null, only after binding will return the corresponding value -         returnLooper.mylooper () = =Looper.getmainlooper (); +     } -  +  A     //determines whether the main process returns true if it is false at      Public Static Booleanismainprocess (Context context) -     { -         returncontext.getpackagename (). Equals (Getprocessname (context)); -     } -  -     //Get process name in      Public StaticString Getprocessname (context context) { -String currentprocessname = ""; to         intPID =Android.os.Process.myPid (); +Activitymanager Manager =(Activitymanager) Context.getsystemservice (context.activity_service); -          for(Activitymanager.runningappprocessinfo processInfo:manager.getRunningAppProcesses ()) { the             if(Processinfo.pid = =pid) { *Currentprocessname =Processinfo.processname; $                  Break;Panax Notoginseng             } -         } the         returnCurrentprocessname; +     } A  the}
1  Packagecom.example.administrator.exceptiontest;2 3 ImportJava.io.PrintWriter;4 ImportJava.io.StringWriter;5 ImportJava.io.Writer;6 7 /**8 * Created by Administrator on 2015/12/9.9  */Ten  Public classBaseuncaughtexceptionhandlerImplementsThread.uncaughtexceptionhandler { One  A @Override -      Public voiduncaughtexception (thread thread, Throwable ex) { -Writer Resultwriter =NewStringWriter (); thePrintWriter PrintWriter =NewPrintWriter (resultwriter); - Ex.printstacktrace (printwriter); -StringBuffer SB =NewStringBuffer (); -Sb.append ("Exception occurred in the main thread" + utils.isinmainthread () + "\ n"); +Sb.append ("Exception occurred in the main process" + utils.ismainprocess (baseapplication.mcontext) + "\ n"); -String errorreport = sb.tostring () +resultwriter.tostring (); +         //this place is best to upload the error logs you have collected to the server for developers to locate and modify problems.  A         //If the main process has an exception, it is left to the system's own default exception handling. Let the user perceive, otherwise the user does not know what the experience is not good at         //you can, of course, define your own special error hints, like some interesting dialog or something. -         if(Utils.ismainprocess (Baseapplication.mcontext)) { - BaseApplication.defaultUncaughtExceptionHandler.uncaughtException (thread, ex); -}Else { -             //if it is a child process exception do not give a hint to kill the child directly the process is best not to let the user perceive - android.os.Process.killProcess (Android.os.Process.myPid ()); in         } -     } to  +  -}

Best practices for Android exception handling

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: [email protected] 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.