Introduction: In the process of developing Android, Debug is absolutely the most painful thing for any developer. In the local development process, if there is an error, you can also view the Logcat information, but the market is a variety of phones, various models of various screen sizes, and even a variety of users, It is difficult for the developer to determine when the user is going to get a bug. Then it is important to collect the released version bug information for the version update and user experience.
In the project to use this demand, spent half a day to study a bit, the specific use of my one by one.
First, the implementation of Uncaughtexceptionhandler.
/** * Collect user phone crash information, upload server * Created by xygy on 2015/7/30. */public class Usererror implements thread.uncaughtexceptionhandler{private String TAG = "Usererror-------->"; Private context context; Gets the system version number of the private int versionmain;//large version number, such as 1 private int versionmini;//A small version number, such as. 1//For formatting dates as part of the log file name privat e DateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss"); Instance private static Usererror usererror; System default processing instance private Thread.uncaughtexceptionhandler Exceptionhandler; Initialize public void init (context context) {This.context = context; Gets the default instance Exceptionhandler = Thread.getdefaultuncaughtexceptionhandler (); Set to the program's default processor Thread.setdefaultuncaughtexceptionhandler (this); }/** * Use Singleton mode * constructor set to private */private Usererror () {} public static Usererror getinstance () { if (Usererror = = null) {return new Usererror (); }else{return usererror; }} @Override public void uncaughtexception (thread thread, Throwable ex) {LOG.V (TAG, "Thread.getname ()-------- ----------------> "+ thread.getname ()); Caughtexception (thread, ex); Call system default processing exceptionhandler.uncaughtexception (thread, ex); If it is not the main thread, force the shutdown thread if (thread.getname (). Equals ("main")) {LOG.V (TAG, "Usererror------------------------> "+ null); }else{}}/** * handles user crash information * @param thread * @param ex */private void Caughtexception (Thr EAD thread, Throwable ex) {final Writer result = new StringWriter (); Final PrintWriter printwriter = new PrintWriter (result); Get the stack information of the tracking, in addition to the system stack information, but also the phone model, system version, compiled version of the unique sign upload stacktraceelement[] trace = Ex.getstacktrace (); stacktraceelement[] Trace2 = new STACKTRACEELEMENT[TRACE.LENGTH+3]; System.arraycopy (Trace, 0, trace2, 0, trace.length); TRACE2[TRACE.LENGTH+0] = new Stacktraceelement ("Android", "MODEL", andRoid.os.Build.MODEL,-1); TRACE2[TRACE.LENGTH+1] = new Stacktraceelement ("Android", "VERSION", Android.os.Build.VERSION.RELEASE,-1); TRACE2[TRACE.LENGTH+2] = new Stacktraceelement ("Android", "fingerprint", Android.os.Build.FINGERPRINT,-1); Append information because the default processing method Ex.setstacktrace (TRACE2) is later recalled; Ex.printstacktrace (PrintWriter); Convert the stack information obtained above into a string and print out string stacktrace = Result.tostring (); Printwriter.close (); LOG.V (TAG, "Usererror------------------------>" + stacktrace); GetVersion (); Jsonarray Jsonarray = new Jsonarray (); Jsonobject jsonobject = new Jsonobject (); try{jsonobject.put ("UserId", New Kfxapp (). GetUserName ());//user ID Jsonobject.put ("DeviceType", "Android:" + Build.VERSION.RELEASE);//Android version information Jsonobject.put ("Versionmain", versionmain);//code Large version Jsonobject.put ("VE Rsionmini ", versionmini);//Code small version jsonobject.put (" Crashstacktrace ", stacktrace);//Crash information Jsonobject.put ("Crashtime", Formatter.format (New Date ())),//Crash time Jsonobject.put ("Phoneinfo", "Manufacturer:" + B Uild. Manufacturer + "mobile phone Model:" + Build.model + "+ build.brand);//Mobile Phone Information jsonobject.put (" Others "," "); Jsonarray.put (Jsonobject); LOG.V (TAG, "json-->" + jsonarray.tostring ()); }catch (jsonexception e) {e.printstacktrace (); }//upload the server code below}/** * Get the version information for the code */private void GetVersion () {}}
It is then initialized in the application OnCreate () method to be applied successfully.
Usererror usererror = Usererror.getinstance (); Usererror.init (Getapplicationcontext ());
Finish
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android collects error messages for released versions (Uncaughtexceptionhandler)