Android crash unified processing mechanism, android Mechanism

Source: Internet
Author: User

Android crash unified processing mechanism, android Mechanism

After the application is released, due to the wide variety of Android models, various problems may occur. At this time, it would be nice to collect and modify the information. Next we will discuss how to handle the phone number of the error message after the program crashes.

Java provides a Thread. UncaughtExceptionHandler interface to handle exceptions during runtime. You only need to implement this interface and override the public void uncaughtException (Thread thread, Throwable ex) method.

Because Application is the first entry for Android app startup, we implement our own Application and let it implement the Thread. UncaughtExceptionHandler interface. Notes for use

1. You need to add your own application class in the Application node of the manifest file, such

 <application        android:name=".CrashApplication"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >

2. In the onCreate method of the implemented Application class, set this class as the default exception processor and add the following code:

Thread. setDefaultUncaughtExceptionHandler (this); 3. Add an implementation in the uncaughtException (Thread thread, Throwable ex) method.


The sample code is as follows to collect information about abnormal mobile phone devices and devices, and save the information to the local device.

 

Public class CrashApplication extends Application implements UncaughtExceptionHandler {// Singleton mode private static CrashApplication INSTANCE; private Context mContext; // private Map used to store device information and exception information <String, string> info = new HashMap <String, String> (); // used to format a date, private SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss"); public CrashApplication () {} public static CrashApplication g EtInstance () {if (INSTANCE = null) {INSTANCE = new CrashApplication ();} return INSTANCE ;}@ Overridepublic void onCreate () {super. onCreate (); mContext = this; // sets the CrashHandler as the default processor Thread of the program. setDefaultUncaughtExceptionHandler (this);} public void uncaughtException (Thread thread, Throwable ex) {// TODO, here you can handle what you want to do when the program crashes // collect the device parameter information collectDeviceInfo (mContext); // Save the log file saveCrashInfo2File (ex );} /*** collect Device parameter information **/public void collectDeviceInfo (Context context) {try {PackageManager pm = context. getPackageManager (); // obtain PackageInfo pi = pm. getPackageInfo (context. getPackageName (), PackageManager. GET_ACTIVITIES); // get the application information, that is, the main Activityif (pi! = Null) {String versionName = pi. versionName = null? "Null": pi. versionName; String versionCode = pi. versionCode + ""; info. put ("versionName", versionName); info. put ("versionCode", versionCode) ;}} catch (NameNotFoundException e) {e. printStackTrace ();} Field [] fields = Build. class. getDeclaredFields (); // reflection mechanism for (Field field: fields) {try {field. setAccessible (true); info. put (field. getName (), field. get (""). toString ();} catch (IllegalArgumentException e) {E. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace () ;}}/ *** Save the exception information to the SD card crash directory */private String saveCrashInfo2File (Throwable ex) {StringBuffer sb = new StringBuffer (); for (Map. entry <String, String> entry: info. entrySet () {String key = entry. getKey (); String value = entry. getValue (); sb. append (key + "=" + value + "\ r \ n");} Writer writer = new StringWriter (); PrintWriter pw = New PrintWriter (writer); ex. printStackTrace (pw); Throwable cause = ex. getCause (); // cyclically write all exception information into writer while (cause! = Null) {cause. printStackTrace (pw); cause = cause. getCause ();} pw. close (); // remember to close String result = writer. toString (); sb. append (result); // save the file long timetamp = System. currentTimeMillis (); String time = format. format (new Date (); String fileName = "crash-" + time + "-" + timetamp + ". log "; if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {try {File dir = new File (Environment. getEx TernalStorageDirectory (), "crash"); if (! Dir. exists () dir. mkdir (); File file = new File (dir, fileName); FileOutputStream fos = new FileOutputStream (file); fos. write (sb. toString (). getBytes (); fos. close (); return fileName;} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} return null ;}}



 

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.