A detailed explanation of how to feedback crash report based on Android application _android

Source: Internet
Author: User
Tags exception handling terminates thread class throwable stringbuffer

Why do I need feedback crash report?

To do the Android application, try to avoid the crash of the program. Although 0 crash is the ultimate goal of programmers chasing, the reality is that programmers can only minimize the occurrence of crash, and almost impossible to completely eliminate crash. Perhaps you think that the robustness of your application is almost perfect and easily withstood the devil's Test in the testing department, but it may not be as fortunate when your application is posted to the marketplace, facing millions and even tens other users.

Based on the above reasons, the general application, must have a crash feedback mechanism. Programmers can improve the current version of the code based on the results of feedback, making the next release more stable.

How to feedback?

Let's look at how to capture the occurrence of crash.

Java has an interface, Uncaughtexceptionhandler, first look at the description.

static interface

Thread.uncaughtexceptionhandler
Invokes the interface of the handler when Thread terminates abruptly because of an exception that is not caught.

Let's look at one of the methods in the thread class.

static void

Setdefaultuncaughtexceptionhandler ( Thread.uncaughtexceptionhandler  eh)
Sets the default handler that is invoked when a thread abruptly terminates because it has not been caught by an exception, and no other handlers are defined for that thread.

Looking at these APIs, we know that we need to implement an interface and then set up handlers in the main thread of the program.

Look at the interface implementation below.

Copy Code code as follows:

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 thread, Throwable ex) {

Collects exception information and sends it to the server

Sendcrashreport (ex);

Wait half a second

try {

Thread.Sleep (500);

catch (Interruptedexception e) {

//
}
Handling Exceptions
HandleException ();
}
private void Sendcrashreport (Throwable ex) {

StringBuffer exceptionstr = new StringBuffer ();

Exceptionstr.append (Ex.getmessage ());

stacktraceelement[] elements = Ex.getstacktrace ();

for (int i = 0; i < elements.length; i++) {

Exceptionstr.append (Elements[i].tostring ());
}

Todo

Send collected crash information to the server

}
private void HandleException () {

Todo

The exception can be processed here.

For example, the prompt user program crashes.

For example, record important information and try to restore the site.

Or simply record important information and kill the program directly.
}
}

Add the following code to the OnCreate (Bundle savedinstancestate) method of the main activity.
Copy Code code as follows:

Thread.setdefaultuncaughtexceptionhandler (New Defaultexceptionhandler (

This.getapplicationcontext ()));

How do I send to the server?

This different project team will have different ways, specifically not discussed here. The caveat is that, in addition to sending the specific information of the exception to the server, you need to send at least version information so that the programmer can determine which version of the exception information on the server appears. In addition to version information, you may also need the phone's SDK version, screen resolution, phone model, and so on information, with this information, you can more fully understand the exception information.

More instructions.

You only need to set up an exception handling class in the main activity, and you do not need to set all the acitivity.

After personal feeling crash, resuming the scene to continue to run is of little significance. After crash, the operation of the program is already unpredictable, with a mistake, to make up for another error, itself will lead to more errors. Suggest or try to avoid the occurrence of crash is more reasonable.

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.