Android client project global exception handling and server data saving, android Exception Handling

Source: Internet
Author: User

Android client project global exception handling and server data saving, android Exception Handling

Love life, love Android! I believe that every Android developer loves his/her work and keeps producing code and solving bugs every day,

Happy, continuous learning, continuous summarization, we should accumulate good habits of writing code by ourselves. Slowly, we will fix

My own things. Below is a problem I encountered when I wrote my own project. I 'd like to share it with you. If you have any insights, please kindly advise. Thank you!

As we all know, Android is based on Java and generates many exceptions that we could not expect when writing code. Every exception is very likely

Causes program crashes, such as the most common NullException, ClassCastException, ClassNotFoundException, and ANR.

Android OS captures these exceptions, processes them in dalvik, and exits the program. The exit interface is unfriendly.

Next, we have our own ideas. If we can catch these exceptions, we can use them to improve the quality of our applications.

After an exception is caught, perform the following operations:

1. define your own processing interface and display it to the customer in a friendly manner to improve user experience

2. Collect error information and modify it in time to improve code Stability

It seems that these two points are simple in text, but when you deploy the Exception Handling Mechanism in your own project, you will know that it applies to our project.

How much help!

Next, let's take a look at how to deploy global exception handling for your project. The steps are as follows:

The first step is to register an Application, which will deal with the Activity during the development of the Application, while the Application is rarely used. Application

Is used to manage the global state of an application, such as loading a resource file. When the Application starts, the Application will first be created before the root

Start the corresponding Activity or Service according to the situation (Intent. This article describes how to register an uncaptured exception processor in the Application.

Step 2: implement the UncaughtExceptionHandler. The UncaughtExceptionHandler is the exception controller not captured by the thread and is used to handle uncaptured exceptions.

Exception. If the program encounters an uncaptured exception, the forced close dialog box is displayed to implement this interface and register it as the default non-captured exception in the program.

In this way, when no exception is captured, you can perform some exception handling operations, such as collecting exception information and sending error reports. When the program is Uncaught

When an exception occurs, this class takes over the program and records the error report.

Step 3: Collect exception information for exception handling operations

Step 4: Save the local exception report for exception handling operations

Step 5: Send the exception information to the server

The preceding five steps are implemented in the exception handling class only in step 1, step 2, step 3, step 4, and Step 5.

Because it is a specific processing process and is important, it is listed separately in this article.

Let's take a look at the code implementation:

Register Application


Implement UncaughtExceptionHandler

When an exception occurs, the uncaughtException method is used for processing. All our controls can be added here.



Collect exception information


Save the exception report locally. This step is very practical. For some exceptions that cannot be captured, we can instantiate and save it in this way.

In the local file, we can see the specific line of the class in the same way as in logcat.

Modification, very easy to use, strongly recommended!


Send exception information to the server


After getting the exception information, we can also set up our own servers to collect the exception information. If you want to save the information, you will find yourself used to it!

I got the exception data and saved it to the local database, as shown in the following example.




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.