How Android applications report crash (zz)

Source: Internet
Author: User

Why do we need feedback on the crash report?

 

Android applicationsProgramTo 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. Based on the feedback, the programmer canCodeTo 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. uncaughtexceptionhandlerEh) 
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 . Act = 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 ){

//

}



// Exception Handling

Handleexception ();



}



Private Void Sendcrashreport (throwable ex ){



Stringbuffer exceptionstr = New Stringbuffer ();

Predictionstr. append (ex. getmessage ());



Stacktraceelement [] elements = ex. getstacktrace ();

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

// The exception can be handled 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 (NewDefaultexceptionhandler (

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.

 

 

Reposted from: How does the Android app report the crash report?

See: Android tips (1): Capture Application runtime exceptions

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.