After the release of the app, there could be a variety of problems due to the wide range of Android models, and it would be nice if we could collect the information and make changes. Here's a discussion of how to handle the error message after the program crashes the phone.
java has provided an interface Thread.uncaughtexceptionhandler to handle exceptions at runtime. Just implement this interface and overwrite the public void uncaughtexception (thread thread, Throwable ex) method.
because application is the first entry for Android app launch, we implement our own application and let him implement thread.uncaughtexceptionhandler interface. There are a few things to note when using
1. The application node in the manifest file needs to be added to your own implementation of application class, such as
<application android:name= ". Crashapplication " android:icon=" @drawable/ic_launcher " android:label=" @string/app_name " android: Theme= "@style/apptheme" >
2. In the implementation of the in the OnCreate method of the application class, set this class as the default exception handler, adding the following code :
Thread.setdefaultuncaughtexceptionhandler (this); 3. Add the implementation in the Uncaughtexception (thread thread, Throwable Ex) method.
The sample code below implements the collection of abnormal mobile device information and exception information and saves the information to a local
public class Crashapplication extends application implements Uncaughtexceptionhandler {//Singleton mode private static Crashapplication instance;private Context mcontext;//used to store device information and exception information private map<string, string> info = new hashmap& Lt String, string> ();//used to format the date as part of the log file name private SimpleDateFormat format = new SimpleDateFormat (" Yyyy-mm-dd-hh-mm-ss ");p ublic crashapplication () {}public static crashapplication getinstance () {if (INSTANCE = = null) { INSTANCE = new Crashapplication ();} return INSTANCE;} @Overridepublic void OnCreate () {super.oncreate (); mcontext = this;// Set the Crashhandler as the program's default processor Thread.setdefaultuncaughtexceptionhandler (this);} public void uncaughtexception (thread thread, Throwable ex) {//TODO, where you can handle what you want to do when the program crashes//collect device parameter information Collectdeviceinfo ( Mcontext);//Save log file Savecrashinfo2file (ex);} /** * Collect device parameter information * */public void Collectdeviceinfo (context context) {try {packagemanager pm = Context.getpackagemanager (); /Get Package Manager packageinfo pi = pm.getpackageinfo (Context.getpackagename (), packagemanager.get_activities)///Get information about the application, that is, the primary 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 exception information to 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 ();// Loop to write all the exception information to writer while (cause! = null) {cause.printstacktrace (PW); cause = Cause.getcause ();} Pw.close ();//Remember to close string result = Writer.tostring (); Sb.append (result);//save file Long Timetamp = System.currenttimemillis ( ); String time = Format.format (new Date ()); String fileName = "crash-" + Time + "-" + Timetamp + ". Log"; if (Environment.getexternalstoragestate (). Equals (ENVIRONMENT.M edia_mounted) {try {file Dir = new File (Environment.getexternalstoragedirectory (), "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;}}
Android program crash Unified processing mechanism