Android detects unknown exceptions and submits them to the server

Source: Internet
Author: User

In Android applications, even if the applications are already on the market, some unknown exceptions may occur. If you can obtain user feedback, this is a good help for our application development.

To achieve this, we need to do the following:

Write a class to implement the uncaughtexceptionhandler interface and override the uncaughtexception method.

Function Description: When an unknown exception occurs, the application is forced to exit, and the application is restarted, the system prompts the user whether to send the error message to the developer.

Public class myuncaughtexceptionhandler implements uncaughtexceptionhandler {

Private Static final string tag = "myuncaughtexceptionhandler ";

// Save the error message to sharepreference
Private Static sharedpreferences bugpreferences;
Private Static sharedpreferences. Editor bugeditor;

Private Static Context mcontext;
Private Static packageinfo;
Private uncaughtexceptionhandler defaultuncaughtexceptionhandler;
Private Static handleprogressdialog progressdialog;

// Save the error cause field
Private Static string bugexiststr = "";

Private Static handler = new handler (){

@ Override
Public void handlemessage (Message MSG ){

Progressdialog. Dismiss ();

}
};

Public myuncaughtexceptionhandler (context ){

Try {
Mcontext = context;
Packageinfo = context. getpackagemanager (). getpackageinfo (
Context. getpackagename (), 0 );
Bugpreferences = context. getsharedpreferences ("bug", 0 );
Bugeditor = bugpreferences. Edit ();
Defaultuncaughtexceptionhandler = thread
. Getdefaultuncaughtexceptionhandler ();
} Catch (namenotfoundexception e ){
E. printstacktrace ();
} Catch (exception e ){
E. printstacktrace ();
}

}

// This method is called when an exception occurs.
Public void uncaughtexception (thread th, throwable t ){
Try {
// Save the bug
Savebugtext (t );
Defaultuncaughtexceptionhandler. uncaughtexception (Th, t );
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (exception e ){
E. printstacktrace ();
}

}

Private void savebugtext (throwable ex) throws filenotfoundexception {

Writer writer = new stringwriter ();
Printwriter = new printwriter (writer );
Ex. printstacktrace (printwriter );
Throwable cause = ex. getcause ();
While (cause! = NULL ){
Cause. printstacktrace (printwriter );
Cause = cause. getcause ();
}
Printwriter. Close ();
Bugeditor. putstring ("bugtext", writer. tostring ());
Bugeditor. Commit ();

}

// When the application is enabled next time, if an unknown exception occurs last time, the dialog box application and user feedback are displayed.
Public static void showbugreportdialog (final context ){

Bugexiststr = context. getsharedpreferences ("bug", 0). getstring (
"Bugtext ","");
If (bugexiststr! = NULL &&! Bugexiststr. Equals ("")){
Alertdialog. Builder = new alertdialog. Builder (context );
Builder. settitle (R. String. bug_report );
Builder. setmessage (R. String. whether_report_to_developer );
Builder. setnegativebutton (R. String. Cancel, new onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){

Finish (DIALOG );
}
});
Builder. setpositivebutton (R. String. Send, new onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){
// Submit the bug to the server
Postbugreportinbackground (context );

Dialog. Dismiss ();
}
});
Alertdialog dialog = builder. Create ();
Dialog. Show ();
}
}

Private Static void postbugreportinbackground (final context ){

Progressdialog = new handleprogressdialog (context );
Progressdialog. Show ();
New thread (New runnable (){
Public void run (){
Postbugreport ();
// Clear the previous bug information
If (bugexiststr! = NULL ){
Bugeditor. putstring ("bugtext ","");
Bugeditor. Commit ();
}

Handler. sendemptymessage (0 );
}
}). Start ();
}

//-----------------------------------------------------------------
/**
* Send bug report.
*/
//-----------------------------------------------------------------
Private Static void postbugreport (){
List <namevaluepair> nvps = new arraylist <namevaluepair> ();
Nvps. Add (New basicnamevaluepair ("device", build. Device ));
Nvps. Add (New basicnamevaluepair ("model", build. Model ));
Nvps. Add (New basicnamevaluepair ("SDK-version", build. version. SDK ));
Nvps. Add (New basicnamevaluepair ("APK-version", packageinfo. versionname ));
Nvps. Add (New basicnamevaluepair ("bug", bugexiststr ));

Try {
Httppost = new httppost (constants. baseurl
+ "C = Main & A = androidcrash ");
Httppost. setentity (New urlencodedformentity (nvps, HTTP. utf_8 ));
Defaulthttpclient httpclient = new defaulthttpclient ();
Httpparams Params = httpclient. getparams ();
Httpconnectionparams. setconnectiontimeout (Params, 5000 );
Httpconnectionparams. setsotimeout (partams, 5000 );
Httpclient.exe cute (httppost );
} Catch (ioexception e ){
E. printstacktrace ();
} Catch (exception e ){
E. printstacktrace ();
}
}

Private Static void finish (dialoginterface DIALOG ){
If (bugexiststr! = NULL ){
Bugeditor. putstring ("bugtext ","");
Bugeditor. Commit ();
}
Dialog. Dismiss ();
}

}

Call where exceptions need to be caught

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

Setcontentview (R. layout. activity_main );
Appapplication = (appapplication) getapplication ();
Appapplication. activites. Add (this );
Initviews ();

Thread. setdefaultuncaughtexceptionhandler (New myuncaughtexceptionhandler (
Mainactivity. This ));
Myuncaughtexceptionhandler. showbugreportdialog (this );
}

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: info-contact@alibabacloud.com 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.