Since it was not long before android development, I have a thorough understanding of android systems and mechanisms. As a result, many inexplicable problems are encountered during development, the most worrying of which is application crash. Application crash may be caused by many reasons, data problems, or improper use of asynchronous AsyncTask, and so on.
I have been thinking about how to know the application crash, and I have never found a solution for what I want to do during application crash. Later, when someone else's application was used to capture crash at an accidental opportunity, he wanted to get the same result. I tried my best to find the sourcecode of this application. I found it was very simple, and I could do it in a few words.
1. Add this sentence in the onCreate method of Activity:
Thread. setDefaultUncaughtExceptionHandler (new MyUncaughtExceptionHandler (this ));
View plain @ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Thread. setDefaultUncaughtExceptionHandler (new UncaughtExceptionHandler (this ));
SetContentView (R. layout. main );
Click2Crash = (Button) findViewById (R. id. clickToCrash );
Click2Crash. setOnClickListener (new Button. OnClickListener (){
@ Override
Public void onClick (View v ){
String text = "aaa ";
System. out. print (text. charAt (10 ));
}});
}
2. Write your own MyUncaughtExceptionHandler implementes UncaughtExceptionHandler and implement the uncaughtException method. In this method, do anything you want to do, for example, send the cause of application crash to developers.
+ Expand sourceview plain
3. Write an Activity of your own and tell the user that you need the exception information and ask the developer to Email it.
View plainpublic class BugReportActivity extends Activity {
Public static final String exceptionMsg = "exceptionMsg ";
Private TextView reportContent;
Private Button sendMailBtn;
Private Button cancelBtn;
Protected void onCreate (Bundle bundle ){
Super. onCreate (bundle );
Thread. setDefaultUncaughtExceptionHandler (new UncaughtExceptionHandler (this ));
SetContentView (R. layout. bug_report );
ReportContent = (TextView) findViewById (R. id. reportContent );
SendMailBtn = (Button) findViewById (R. id. sendMail );
CancelBtn = (Button) findViewById (R. id. cancel );
String sw = getIntent (). getStringExtra (exceptionMsg );
ReportContent. setText (sw );
InitHandler ();
}
Private void initHandler (){
SendMailBtn. setOnClickListener (new Button. OnClickListener (){
@ Override
Public void onClick (View v ){
Toast. makeText (v. getContext (), "Email will be sent to our helpdesk.", Toast. LENGTH_LONG). show ();
}
});
CancelBtn. setOnClickListener (new Button. OnClickListener (){
@ Override
Public void onClick (View v ){
Finish ();
}
});
}
}
I have always been reading others' blogs to learn things. Today I am sending a blog for the first time. Sorry for any bad things. :-)
I found a GoogleProject that focuses on this Application Crash Report for Android, ACRA for short. You can find relevant content on the link below.