Android uses crashhandler to get the crash information of the application

In the process of daily development, crash should inevitably occur. No matter how perfect your program is, it is impossible to completely avoid crash. It may be due to the underlying bugs of Android, insufficient model adaptation or poor network conditions. When a crash occurs, the system will kill the running program. The phenomenon is to flash back or remind the user that the program has stopped running, which is very unfriendly to the user and we don't want to see. Earlier, when the user crashes, our developers can't know why the program crashes, even if we want to solve the bug, However, there is often nothing you can do because you can't know the crash information of the user at that time. Fortunately, andorid provides methods to deal with such problems. Next, let's see what Android provides us to solve this difficult problem

1、 Setdefaultuncaughtexceptionhandler in thread class

In fact, this method can solve the crash problem of our application, set the default exception handler of the system, and when the system crashes, the system will call back the uncaughtexception method of uncaughtexceptionhandler. The exception information can be obtained in the uncaughtexception method, and you can choose to store the exception information, You can choose the storage method, and then upload the crash information to the server through the network at the appropriate time, so that our developers can analyze the user crash scene and repair it in the later version. We can also pop up a dialog box when the crash occurs, tell the user that the program crashed, and then exit

2、 Implement your own exception capture class

1) Create an exception handler named crashhandler with the following code

It can be seen from the above code that when the application crashes, the crashhandler class will write the exception information and device information to the SD card. Here, you can also process it according to your project needs. For example, you can also store it in the database, and then hand the exception to the system for processing. The system will help us stop the program. If the system does not have a default exception handling mechanism, Then you can stop by yourself. Of course, you can choose to upload the exception information to the server. We do not implement this logic here. In actual development, you need to upload the exception information to the server

3、 How to use crashhandler

In fact, using crashhandler is also very simple. We can set crashhandler during application initialization, as shown below:

Through the above operations, our program can capture crash and view the user's crash information from the server. I'll write here today to recommend a good book: Android development art. The author is Ren Yugang. I believe you all know that the content of this book is very good and worth reading, It is more suitable for students with a certain Android foundation. It will be the National Day holiday soon. I wish you a happy National Day

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>