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