How Android applications report crash reports

Source: Internet
Author: User

 

Why do we need feedback on the crash report?

 

For Android applications, we should try to avoid crash. Although zero-crash is the ultimate goal pursued by programmers, the reality is that programmers can only minimize the occurrence of crash, and it is almost impossible to completely eliminate it. Maybe you think that your application is almost perfect and can easily withstand the devil's test in the testing department. But when your application is released to the market, it may not be so lucky to face millions or even tens of millions of users.

For the above reasons, a crash feedback mechanism is required for general applications. Programmers can improve the code of the current version based on the feedback to make the next version more stable.

 

How to provide feedback?

 

First, let's take a look at how to capture the occurrence of crash.

 

There is an interface in Java, uncaughtexceptionhandler. first look at the description.

static interface

Thread. uncaughtexceptionhandler

WhenThreadWhen an uncaptured exception is suddenly terminated, the handler interface is called.

 

Let's take a look at a method in the Thread class.

static void

Setdefaultuncaughtexceptionhandler(Thread. uncaughtexceptionhandler eh)

Sets the default handler that is called when the thread is suddenly terminated because no exception is caught and no other handler is defined for the thread.

 

After reading these APIs, we can see that we need to implement such an interface and then set the handler in the main thread of the program.

 

See the following interface implementation.

 

Package COM. arui. framework. android. exception; import Java. lang. thread. uncaughtexceptionhandler; import android. content. context;/*** default exception handler for all activities. ** @ author http://blog.csdn.net/arui319 * @ version 2011/12/01 **/public class defaultexceptionhandler implements uncaughtexceptionhandler {private context act = NULL; Public defaultexceptionhandler (context Act) {This. A Ct = act ;}@ override public void uncaughtexception (thread, throwable ex) {// collect exception information and send it to the server sendcrashreport (Ex); // wait for half a second try {thread. sleep (500);} catch (interruptedexception e) {//} // handle exception handleexception ();} private void sendcrashreport (throwable ex) {stringbuffer exceptionstr = new stringbuffer (); predictionstr. append (ex. getmessage (); stacktraceelement [] elements = ex. getstacktr ACE (); For (INT I = 0; I <elements. length; I ++) {predictionstr. append (elements [I]. tostring ();} // todo // send the collected crash information to the server} private void handleexception () {// todo // you can handle the exception here. // For example, the user program crashes. // For example, record important information and try to restore the site. // Or simply record important information and then directly kill the program. }}

 

 

Add the following code to the oncreate (bundle savedinstancestate) method of the main activity.

 

           Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(                  this.getApplicationContext()));

 

 

How to send to the server?

 

Different project teams may have different approaches, which are not discussed here. It should be noted that in addition to sending the specific exception information to the server, at least the version information should be sent so that the programmer can determine the version of the exception information on the server. In addition to the version information, you may also need the mobile phone SDK version, screen resolution, mobile phone model, and other information. With this information, you can have a more comprehensive understanding of the exception information.

 

For more information, see.

 

You only need to set an exception handling class in the main activity, and do not need to set it in all the acitifiers.

 

I personally feel that it is of little significance to resume the on-site operation after a crash occurs. After the crash, the running status of the program is unpredictable. Using One error to make up for another error will lead to more errors. It is recommended to avoid crash occurrence as much as possible.

 

---------------------------------------------------------------------------

GL (arui319)

Http://blog.csdn.net/arui319

<This article can be reproduced, but please keep the above author information. Thank you.>

---------------------------------------------------------------------------

 

 

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.