Android Crash capture

Source: Internet
Author: User
Tags throwable

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:




Android Crash capture

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: 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.