Love life, Love android! Believe that every Android developer loves their work, the production of code every day, constantly solve the bug,
Enjoy, continuous learning, continuous summing up, we should accumulate their own code of good habits, slowly, we will be fixed completely belong to
Their own things, the following is their own writing projects, encountered a problem, to share with you, there is no point of view, but also please advise, thank you!
As we all know, Android is based on Java, it will produce a lot of things we do not anticipate when writing code, and every exception is likely
Cause our program to crash, like the most common nullexception, classcastexception, classnotfoundexception, ANR, etc.
, while Android itself's OS will catch these exceptions, handle them in Dalvik, then quit the program and exit the interface is also very unfriendly, in this case
, we have our own ideas, if we can capture these anomalies, we can use them to improve the quality of our applications, we can
After catching the exception, do the following:
1, define their own processing interface, friendly display to the customer, improve the user experience
2, collect error information, timely modification, improve the stability of the code
The light from the text looks, these two points are very simple, but when you own the exception handling mechanism in your project, you know it on our project
How much help it brings!
Let's take a look at how to give your project a global exception, step into the following steps:
The first step is to register application, which will interact with the activity when developing the application, while the application uses relatively little. Application
is used to manage the global state of an application, such as loading a resource file. When the application starts, application is created first, and then the root
According to the situation (Intent), initiate the corresponding activity or service. This article registers an uncaught exception handler in application.
The second step is to implement Uncaughtexceptionhandler,uncaughtexceptionhandler, which is a thread that does not capture the exception controller to handle
To be abnormal. If the program has an uncaught exception, the Force Close dialog box is implemented by default to implement the interface and register as the default Uncaught xor in the program
Often handled so that when an uncaught exception occurs, you can do some exception handling operations such as collecting exception information, sending error reports, and so on. When the program occurs uncaught
When an exception is made, the class is taken over by the program and a record is sent for error reporting.
The third step is to collect exception information for exception handling operations
Fourth step, exception handling operation local Save exception Report
Fifth, exception handling sends exception information to the server
The above five steps, if the general classification, only the first step, the second step, the third step behind, the fourth step, fifth step is implemented in the exception handling class, only
is because it is a specific process, more important, so this article is listed separately.
Here's a look at the code implementation:
Register Application
Implement Uncaughtexceptionhandler
When an exception occurs, the Uncaughtexception method is processed and all of our controls can be added here.
Collecting exception information
Local Save exception Report , This step is very practical, some catch exception, we can instantiate it in such a way, save
In the local file, inside of the detailed information, we can like in the Logcat, see it specifically out in which class which row, thus making
Modified, very useful, highly recommended!
Sending exception information to the server
Get the exception information, we can also set up their own server, collect abnormal information, how to save, look at everyone's own habits!
I'm here to get the exception data, saved to the local database, as the following example.
Android client project global exception handling and server data saving