public class Errorreport implements Thread.uncaughtexceptionhandler{public static final String TAG = "Gouminerrorreport" ;p rivate Context mcontext;//system default Uncaughtexception processing class private Thread.uncaughtexceptionhandler mdefaulthandler; Private String Logstr;public errorreport (context context) {Mcontext = context;//gets the system default Uncaughtexception Processor Mdefaulthandler = Thread.getdefaultuncaughtexceptionhandler (); Thread.setdefaultuncaughtexceptionhandler (this);} @Overridepublic void uncaughtexception (thread thread, Throwable ex) {gmlog.i (TAG, "uncaughtexception"); GMLOG.I (TAG, Ex.getlocalizedmessage ()); Ex.printstacktrace (); logstr = Getstacktrace (ex);//Use Toast to display exception information new Thread ( {@Overridepublic void Run () {looper.prepare (); Toast.maketext (Mcontext, "Sorry, the program has an exception and is about to exit.) ", Toast.length_short). Show (); Sendreport (LOGSTR); Looper.loop ();}}. Start ();//If the user does not handle then let the system default exception handler handle if (Mdefaulthandler! = null) {mdefaulthandler.uncaughtexception (thread, ex);}} private void Sendreport (String stackTrace) {gmlog.i (TAG, "sEndreport "); HttpClient HttpClient = new Defaulthttpclient (), final httppost httppost = new HttpPost (Merrorreportreq.geturl ()); try { Httppost.setentity (New Stringentity (StackTrace)));} catch (Unsupportedencodingexception E1) {e1.printstacktrace ();} HttpResponse httpresponse;try {httpresponse = Httpclient.execute (httppost); final int statusCode = Httpresponse.getstatusline (). Getstatuscode (); GMLOG.I (TAG, "Sendreport response StatusCode is:" + StatusCode); if (StatusCode = = HTTPSTATUS.SC_OK) {String response = Ent Ityutils.tostring (Httpresponse.getentity (), HTTP. UTF_8); GMLOG.I (TAG, "sendreport response:" + response);}} catch (Clientprotocolexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {gmlog.i ( TAG, "Sendreport finally"), if (httpclient! = null) {Httpclient.getconnectionmanager (). shutdown (); httpclient = null;}}} Private String getstacktrace (throwable th) {final Writer result = new StringWriter (); final printwriter printwriter = new P Rintwriter (result);//IF The exception is thrown in a background thread inside//Asynctask and then the actual exception can is found with Getcause Throwable cause = Th;while (cause! = null) {cause.printstacktrace (printwriter); cause = Cause.getcause ();} Final String stacktraceasstring = result.tostring ();p rintwriter.close (); return stacktraceasstring;}}
For Android exception exit, the main use of the interface Thread.uncaughtexceptionhandler
public class MyApplication extends application {errorreport errorreport; @Overridepublic void OnCreate () {super.oncreate (); errorreport = new Errorreport (this);}}
Back up several links:
Https://github.com/ACRA/acra
Https://github.com/ACRA/acra/wiki/AdvancedUsage#data-encoding-formjson
http://blog.csdn.net/moruite/article/details/7397000
Http://www.cnblogs.com/lee0oo0/archive/2012/11/28/2793052.html
Android Crash capture