Explain in detail the use of Android strictmode strict mode

Android 2.3 provides a debugging feature called strict mode, which Google says has benefited hundreds of Google applications on Android. What does it do? It will report policy violations related to threads and virtual machines. Once a policy violation is detected, you will get a warning, which includes a stack trace to show where your application has violated. You can force a warning instead of crash, or you can log only the warning and let your application continue. Strictmode is a very useful class, which can be easily applied to check the performance and problems of Android applications. When this mode is enabled, developers can well check the potential problems in the application.

Strictmode is divided into the following two strategies:

1. Thread monitoring strategy

(1) Setthreadpolicy(): this policy will be applied to the current thread

(2) Detectdiskreads(): monitors disk reads

(3) Detectdiskwrites(): monitors disk writes

(4) Detectnetwork (): monitors network access

(5) Detectall(): detects all functions of the current thread

(6) Penaltylog(): indicates that the warning is output to logcat. You can also use other or add new penalty functions. For example, if penaltydeath() is used, the application will crash once the strictmode message is written to logcat.

You don't need to open the strict mode frequently. You can open it in the oncreate() function of the activity, or you can set the strict mode in the oncreate() function of the application derived class. Any code running in the thread can set strict mode, but you really only need to set it once, once is enough.

2. VM virtual machine monitoring strategy

VM focuses on the following categories:

(1) Memory compromised activity object

(2) SQLite object with memory leak

(3) Any other similar closable object with a memory leak

The activity object with memory leak and SQLite object with memory leak are easy to understand. The so-called "any other similar closable object" check is mainly to monitor the objects that should be released, such as the objects that should call the close () method.

3. It should be said that most of the rule warnings generated by strictmode should be observed, but sometimes not all the generated information indicates that your program is wrong. For example, fast disk reading and writing in the main thread of the application will not have much impact on the performance of the application, or some debugged code violates the set rules in the debugging stage, which can be ignored.

There are two methods to ignore rules. One is to simply comment out the strictmode code in the code. The other is to add corresponding code when and where it needs to be ignored to make the system stop using these rules for inspection. When the developer thinks it is necessary to check, re apply these rules, such as:

Here, first save the current policy rules with old, and then do write something to disk; Here, some quick read and write operations to the disk are performed, and finally these rules are re enabled.

summary

The above is the use method of Android strictmode strict mode introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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