Due to the diversity of Android devices and a large number of third-party Android systems, we cannot test all device scenarios. Therefore, it is almost impossible to develop a completely bug-free application, so when the application forces Close on the user's device, can we capture this error, record the user's device information, and then let the user choose whether to feedback the stack information? Through this bug feedback method, we can fix bugs in a targeted manner.
When our application causes Force Close due to a running exception, you can set the main thread's UncaughtExceptionHandler to capture the stack information of the running exception. At the same time, you can send an email to feedback stack information to us. The following is the implementation code:
Click here to download the code
Example: click the button to trigger an NullPointerException running exception. This example allows you to capture running exceptions and send email feedback to devices and stack information.
Interface 1 (exception triggered)
Interface 2 (send stack information)
TestActivity. java
View plain
Package com. zhuozhuo;
Import java. io. PrintWriter;
Import java. io. StringWriter;
Import java. lang. Thread. UncaughtExceptionHandler;
Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Build;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. EditText;
Import android. widget. TextView;
Public class TestActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Thread. setDefaultUncaughtExceptionHandler (new UncaughtExceptionHandler () {// set a handler for the main Thread to handle runtime exceptions
@ Override
Public void uncaughtException (Thread thread, final Throwable ex ){
StringWriter sw = new StringWriter ();
PrintWriter pw = new PrintWriter (sw );
Ex. printStackTrace (pw );
StringBuilder sb = new StringBuilder ();
Sb. append ("Version code is ");
Sb. append (Build. VERSION. SDK_INT + "\ n"); // The Android VERSION of the device
Sb. append ("Model is ");
Sb. append (Build. MODEL + "\ n"); // Device MODEL
Sb. append (sw. toString ());
Intent sendIntent = new Intent (Intent. ACTION_SENDTO );
SendIntent. setData (Uri. parse ("mailto: csdn@csdn.com"); // send an exception to the csdn@csdn.com mailbox
SendIntent. putExtra (Intent. EXTRA_SUBJECT, "bug report"); // subject
SendIntent. putExtra (Intent. EXTRA_TEXT, sb. toString (); // stack information
StartActivity (sendIntent );
Finish ();
}
});
FindViewById (R. id. button). setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Integer a = null;
A. toString (); // The nullpointer runtime error is triggered.
}
});
}
}
Main. xml
View plain
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/hello"/>
<EditText android: id = "@ + id/editText1" android: layout_width = "match_parent"
Android: text = "An exception occurred when you click the button to trigger the runtime" android: layout_height = "wrap_content"
Android: layout_weight = "1" android: gravity = "top"> </EditText>
<Button android: text = "button" android: id = "@ + id/Button"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"> </Button>
</LinearLayout>
Author's "lzc column"