Capture Application Crash in Android Activity

Source: Internet
Author: User

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.

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