The example in this article describes how the Android implementation captures unknown exceptions and submits them to the server. Share to everyone for your reference, specific as follows:
In Android applications, even if the application is already on the market, but sometimes there are some unknown exceptions, if you can get feedback from users, then the development of our application is a good help
In order to achieve this effect, we need to do the following work
Write a class to implement the Uncaughtexceptionhandler interface and rewrite the Uncaughtexception method
Function Description: When the application has an unknown exception, the application forced to quit, the application to start again, prompts the user whether the error feedback to the developer
public class Myuncaughtexceptionhandler implements Uncaughtexceptionhandler {private static final String TAG = "Myunca
Ughtexceptionhandler ";
Save the error message to sharepreference private static sharedpreferences bugpreferences;
private static Sharedpreferences.editor Bugeditor;
private static context Mcontext;
private static PackageInfo PackageInfo;
Private Uncaughtexceptionhandler Defaultuncaughtexceptionhandler;
private static Handleprogressdialog ProgressDialog;
The field that holds the cause of the error private static String bugexiststr = ""; private static Handler Handler = new Handler () {@Override public void Handlemessage (msg) {Progress
Dialog.dismiss ();
}
};
Public Myuncaughtexceptionhandler {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 (); When an exception occurs, this method is called public void uncaughtexception (Thread th, Throwable t) {try {//save bug Savebug
Text (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 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 (); }//Next timeWhen the application is turned on, if the last time an unknown exception is generated, the dialog box is applied to the user feedback public static void Showbugreportdialog (final context) {bugexiststr = Conte
Xt.getsharedpreferences ("bug", 0). getString ("Bugtext", ""); if (bugexiststr!= null &&!bugexiststr.equals ("")) {Alertdialog.builder 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 w
Hich) {//submit bug to 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 ();
Clears the previous bug information out of the IF (bugexiststr!= null) {bugeditor.putstring ("Bugtext", "");
Bugeditor.commit ();
} handler.sendemptymessage (0);
}). Start ();
}/** * Send Bug.
* * 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 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 (params, 5000);
Httpclient.execute (HttpPost);
catch (IOException e) {e.printstacktrace ();
catch (Exception e) {e.printstacktrace (); } private static void Finish (Dialoginterface dialog) {if (bugexiststr!= null) {bugeditor.putstring ("bug
Text "," ");
Bugeditor.commit ();
} Dialog.dismiss ();
}
}
Called where the exception is needed to catch the
@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);
}
More interested readers of Android-related content can view the site: Android Development Primer and Advanced tutorials, the Android View View tips Summary, the activity tips summary of Android programming, Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Resource Operation tips Summary and the "Android Controls usage Summary"
I hope this article will help you with the Android program.