After the app was released, there was always feedback that crash had occurred, but I couldn't locate the problem because I couldn't get the log. Later found that we should collect crash logs and upload them to the server.
There are a lot of three-party agencies in the country to provide the crash collection of the SDK, we can directly use, for example, I used to do the app is BUGHD (http://bughd.com/) services provided.
But what is the principle of collapse collection? After searching, we found that using Uncaughtexceptionhandler in Java, we can Thread.setdefautuncaughtexceptionhandler () Set our own uncaughtexceptionhandler. It is actually an interface, the implementation method can be.
I wrote a demo:
Import Android.os.looper;import android.widget.toast;import Java.io.file;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.printwriter;import Java.io.stringwriter;import Java.io.Writer;import Java.text.simpledateformat;import java.util.date;import java.util.locale;import java.util.Map;/** * Created by ROWANDJJ on 2015/5/19. * * Crash Log Collection */public class Crashhandler implements thread.uncaughtexceptionhandler{private static Crashhandler Sinstanc e = null; private static Object lock = new Object (); Private Thread.uncaughtexceptionhandler Mdefaultexceptionhandler; Private Crashhandler () {} public static Crashhandler getinstance () {if (sinstance = = null) { Synchronized (lock) {if (sinstance = = null) Sinstance = new Crashh Andler (); }} return sinstance; } public void Register () {Mdefaultexceptionhandler = Thread.getdefaultuncaughtexcePtionhandler (); Thread.setdefaultuncaughtexceptionhandler (this); } @Override public void uncaughtexception (thread thread, Throwable ex) {//Generate log final String Filena me = Cachelog (ex); New Thread (New Runnable () {@Override public void run () {LOOPER.PR Epare (); if (filename = null) Toast.maketext (Appenv.getappcontext (), "The program crashed: (\ n \ nthe crash log was saved to" +filename+ ", Toast.lengt H_short). Show (); Looper.loop (); }}). Start (); try {thread.sleep (2000); } catch (Interruptedexception e) {e.printstacktrace (); } mdefaultexceptionhandler.uncaughtexception (Thread,ex); } private String Cachelog (Throwable ex) {if (ex = = null) return null; map<string, string> hardwareinfo = Hardwareutils.gethardwareinfo (); StringBuilder buffer = new StringBuilder (); For (map.entry<string, string> me:hardwareInfo.entrySet ()) {Buffer.append (Me.getkey () + ":" + M E.getvalue () + "\ n"); } buffer.append ("Packname:" + appenv.getpackagename () + "\ n"); Buffer.append ("Versionname:" + appenv.getversionname () + "\ n"); Buffer.append ("Versioncode:" + appenv.getversioncode () + "\ n"); Writer writer = new StringWriter (); PrintWriter printwriter = new PrintWriter (writer); Ex.printstacktrace (PrintWriter); Throwable cause = Ex.getcause (); while (cause! = null) {cause.printstacktrace (printwriter); Cause = Cause.getcause (); } printwriter.close (); String result = Writer.tostring (); Buffer.append (result); Long timestamp = System.currenttimemillis (); SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss", Locale.china); String time = Formatter.format (new Date ()); String fileName = "crash-" + Time + "-" + timestamp + ". Log"; File File = new file (Basicutils.getstoredir (), filename); try {if (!file.exists ()) file.createnewfile (); FileOutputStream out = new FileOutputStream (file); Out.write (Buffer.tostring (). GetBytes ()); Out.close (); LOGGER.D (This,file.getabsolutepath ()); return File.getabsolutepath (); } catch (IOException e) {e.printstacktrace (); } return null; }}
And then call it in your app's application.
Crashhandler.getinstance (). Register ();
Can.
When the app appears crash, it will save the log locally. If you want to upload to the server, add an HTTP upload module.
Part of the code is not posted, everyone's own brain repair.
"Android Notes" Crash log Collection