Detailed explanation and examples of how to obtain exception information when crashing in Android

How to get exception information when crashing in Android

preface:

As we all know, Android applications will inevitably crash. No matter how perfect your program is, you can't completely avoid crash. It may be due to the bugs at the bottom of the Android system, insufficient model adaptation or poor network conditions. When a crash occurs, the system will kill your program, which shows that the program has flashed back or stopped running. This is very unfriendly to users and is also something developers don't want to see. What's worse, when a user crashes, developers can't know what the program crashes. Even if you want to solve the crash, But because you can't know the crash information of the user at that time, there's nothing you can do. Is this true? In fact, Android has methods to deal with such problems. Please see a method in the thread class below

From the literal meaning of the method, it seems that this method can set the default exception handler of the system. In fact, this method can solve the common crash problems in applications. When a crash occurs, we can capture the exception information, store the exception information in the SD card, and then upload the crash information to the server through the network at an appropriate time, so that developers can analyze the user crash scenario and repair this kind of crash in later versions. We can also pop up a notification to tell the user that the program has crashed when a crash occurs, and then exit. This is a little milder than flash back.

step

1. Create an exception handler

Create a new class, such as crashhandler. Java. The code is as follows. I won't explain the code. The comments are quite clear

2. Add a default exception handler for the UI thread

//A member of the thread class that identifies the default exception event handler

private static UncaughtExceptionHandler defaultUncaughtHandler;

Here is the question of where to add it. It is noted from the source code that this defaultuncaughthandler is a static member of the thread class. Therefore, it is reasonable for us to set exception handling for any thread. All threads should be able to share this exception handler. This is my guess. It has not been verified, but it doesn't matter, There is a view that we all agree on: adding an exception programmer to the main thread, that is, the UI thread. In order to add exception handler in UI thread, we recommend that you add it in application instead of activity. Application identifies the whole application and is the first to start in the Android declaration cycle, earlier than any activity, service, etc.

effect

After the above two steps, our program can handle any exception. We are no longer afraid of program crash, and we can view the user's crash information from the server. Let's simulate the crash and see how our program handles it.

We add a click event to the button and throw a runtime exception artificially in onclick. At this time, the program will crash. Let's see what the exception handler has done for us. As can be seen from the following pictures, the exception handler creates a log file for us. When you open the log file, you can see the information of the mobile phone and the call stack when the exception occurs. With these, developers can locate the problem well. From the log call stack below, we can see that the runtimeException occurred in line 28 of crashactivity. Then look at our code and find that line 28 is the exception we think thrown. From now on, add a default exception handler to your application.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>