Android Global Exception handling

Source: Internet
Author: User

In the development of Android project, we all know that if the program error, will pop up a forced exit popup box, this itself is not a problem, but this UI is too ugly, not to mention the user can not accept, even we ourselves may not accept. Although we always go through a careful test when releasing a program, we will inevitably encounter unexpected errors.

Today comes the process of defining a program error, similar to the iphone's flash-back. (Although the user is not willing to see the flash back, but in the user experience is significantly better than the original pop-up window)

Nonsense not much to say, directly on the code:

Crashhandler
/*** Custom Exception handling class, implementation of Uncaughtexceptionhandler interface **/   Public classCrashhandlerImplementsUncaughtexceptionhandler {//The requirement is that the entire application has only one Mycrash-handler    Private StaticCrashhandler INSTANCE; Privatecontext Context; //1. Method of structuring privatization    PrivateCrashhandler () {} Public Static synchronizedCrashhandler getinstance () {if(INSTANCE = =NULL) INSTANCE=NewCrashhandler (); returnINSTANCE; }     Public voidinit (Context context) { This. Context =context; }               Public voiduncaughtexception (Thread arg0, Throwable arg1) {System.out.println ("The program is dead."); //in this can be the user mobile phone information and exception information capture and upload, because umeng in this aspect has a very API interface to call, it is not considered//Kill the current programandroid.os.Process.killProcess (Android.os.Process.myPid ()); }  }
Crashapplication
 /**   * when you develop your application, you will interact with the activity,  The application used relatively little.  * Application is used to manage the global state of an application, such as loading a resource file.  * When the application starts, application is created first, and then the activity or service is started according to the situation (Intent).  * In this document, an uncaught exception handler is registered in application.  */ public  class  crashapplication  application {@Override  public  void OnCreate () { super  .oncreate ();          Crashhandler handler  = Crashhandler.getinstance ();        Handler.init (Getapplicationcontext ());      Thread.setdefaultuncaughtexceptionhandler (handler); }  }
Register in Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"?> <manifest  xmlns:android= "http://schemas.android.com/apk/res/ Android "Package      =" Org.wp.activity "android:versioncode=" 1 "android:versionname=" 1.0 ">      < Application android:icon= "@drawable/icon" android:label= "@string/app_name"          android:name= ". Crashapplication "android:debuggable=" true ">          <activity android:name=". Mainactivity "android:label=" @string/app_name ">              <intent-filter>                  <action android:name=" Android.intent.action.MAIN "/>                  <category android:name=" Android.intent.category.LAUNCHER "/>              </intent-filter>          </activity>      </application>      <uses-sdk android:minsdkversion = "8"/>  </manifest>

At this point, you can test that the program will flash back in error and kill the background process. Of course, you can also customize some of the more friendly error UI hints to further enhance the user experience.

Android Global 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.