Android exception handling Best Practices _android

Source: Internet
Author: User
Tags exception handling stringbuffer

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

1. Can upload error messages 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, such as the primary thread, can help the programmer locate

3. Preferably include mobile phone hardware and software information.

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

5. The exception that is thrown by the subprocess is best not to be perceived by the user. such as push and so on, and user-perceived weakness associated with this. It is best to kill the exception directly. Do not hand over to the system for processing.

The code below.

 package com.example.administrator.exceptiontest;
Import Android.app.ActivityManager;
Import android.app.Application;

Import Android.content.Context;
 /** * Created by the Administrator on 2015/12/9.
  * * Public class Baseapplication extends application {public static context Mcontext;

  The default exception handling public static Thread.uncaughtexceptionhandler Defaultuncaughtexceptionhandler;
    @Override public void OnCreate () {super.oncreate ();
    Mcontext = this;
    First get the default exception handling Handler Defaultuncaughtexceptionhandler = Thread.getdefaultuncaughtexceptionhandler ();
  Thread.setdefaultuncaughtexceptionhandler (New Baseuncaughtexceptionhandler ()); }


}
Package com.example.administrator.exceptiontest;
Import Android.app.ActivityManager;
Import Android.content.Context;

Import Android.os.Looper;
 /** * Created by the Administrator on 2015/12/9. * * public class Utils {/** * To determine whether to perform in the main thread if it returns true instead of returning false */public static Boolean Isinmainthread () {/ /Notice this place we can't be sure to get Mylooper's value. For example, your thread does not bind the message loop//that your mylooper will return must be null, only the binding will return the corresponding value returns Looper.myloop
  ER () = = Looper.getmainlooper (); }//Determine if the main process returns true if it is or returns false public static Boolean ismainprocess (context context) {return CONTEXT.GETPA
  Ckagename (). Equals (Getprocessname (context));
    //Get Process name public static string Getprocessname (context context) {string currentprocessname = "";
    int pid = Android.os.Process.myPid ();
    Activitymanager manager = (Activitymanager) context.getsystemservice (Context.activity_service); For (Activitymanager.runningappprocessinfo processInfo:manager.getRunningAppProcesses ()) {if (proceSsinfo.pid = = pid) {currentprocessname = Processinfo.processname;
      Break
  } return currentprocessname;
 }

}

Package com.example.administrator.exceptiontest;
Import Java.io.PrintWriter;
Import Java.io.StringWriter;

Import Java.io.Writer;
 /** * Created by the Administrator on 2015/12/9. */public class Baseuncaughtexceptionhandler implements Thread.uncaughtexceptionhandler {@Override public void Uncau
    Ghtexception (thread thread, Throwable ex) {Writer resultwriter = new StringWriter ();
    PrintWriter printwriter = new PrintWriter (resultwriter);
    Ex.printstacktrace (PrintWriter);
    StringBuffer sb = new StringBuffer ();
    Sb.append ("An exception occurred in the main thread" + utils.isinmainthread () + "\ n");
    Sb.append ("An 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 information log to the server so that developers could locate and modify the problem. If the main process has an exception, then give the system the default exception handling to do it.
      Let the user perceive that otherwise the user does not know what the experience is not good//You can of course also define specific error cues for yourself, such as some interesting dialog what if (utils.ismainprocess (Baseapplication.mcontext)) { Baseapplication.defaultuncaughtexceptionhandler. uncaughtexception (thread, ex);
    else {//If the exception is a subprocess do not give the prompt to kill the child the process is best not to let the user perceive the android.os.Process.killProcess (Android.os.Process.myPid ());
 }
  }


}

The above is the entire contents of this article, I hope to learn more about Android software programming help.

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.