Detailed explanation of Android strict mode

Strictmode class is a tool class introduced by Android 2.3 (API 9). It can be used to help developers find some non-standard problems in the code, so as to improve the application response ability. For example, if a developer performs network operations or file system operations in the UI thread, these slow operations will seriously affect the responsiveness of the application, and even an anr dialog box will appear. In order to find these easily overlooked problems in development, we use strictmode. The system detects violations of the main thread and makes corresponding responses, and finally helps developers optimize and improve code logic.

Official website documents: http://developer.android.com/reference/android/os/StrictMode.html

What can strictmode detect

The strict mode mainly detects two major problems, one is the thread policy, that is, treadpolicy, and the other is the VM policy, that is, vmpolicy.

Threadpolicy thread policy detection

Vmpolicy virtual machine policy detection

working principle

In fact, the implementation principle of strictmode is also relatively simple. Taking IO operation as an example, it is mainly monitored during open, read, write and close. The libcore.io.blockguardos file is the monitoring place. Take open as an example to monitor as follows.

The implementation code of onreadfromdisk () method is located in strictmode.java.

Common usage

The strict mode can be enabled in the oncreate method of application or activity and other components. In order to better analyze the problems in the application, it is recommended to put it in the oncreate method of the application.

Among them, we only need to use strictmode in the development version of the app, and avoid using strictmode in the online version. Here, a boolean variable dev is defined_ Mode to control.

The methods introduced in Android 3.0 include detectcustomslowcalls() and noteslowcode(), which are used to detect slow or potentially slow code executing in applications.

View report results

There are many forms of reporting violations in strict mode, but to analyze specific violations, you still need to check the log. Filter strictmode under the terminal to get the specific stacktrace information of violations.

Of course, you can also choose the pop-up form to briefly remind developers

Pop up warning

Detailed explanation of threadpolicy

The main methods of strictmode.threadpolicy.builder are as follows

Detectnetwork() is used to check whether there are network request operations in the UI thread

Detect the network request case in the UI thread:

After operation, the following warnings are triggered:

Detectdiskreads() and detectdiskwrites() are disk read / write checks

Disk read / write check case:

After operation, the following warnings are triggered:

Noteslowcall performs time-consuming checks on

Starting from API 11, strictmode allows developers to customize some time-consuming call violations. This customization is applicable to custom task execution classes. For example, we have a class for task processing, taskexecutor.

First, you need to track the time consumption of each task. If it is greater than 500 milliseconds, you need to prompt the developer. Noteslowcall can realize this function. Modify the code as follows

Perform a task that takes 2000 milliseconds

Note that ~ duration = 20 ms is not the execution time of the time-consuming task, but our custom information MSG = slowcall cost = 2000 contains the real time-consuming.

Penaltydeath(), when the violation condition is triggered, directly crash the current application.

Penaltydeathonnetwork(), crash the current application when a network violation is triggered.

Penaltydialog(), when a violation is triggered, the violation information dialog box is displayed.

Penaltyflashscreen() will cause the screen to flash, but ordinary devices may not have this function.

penaltyDrop@R_ 566_ 2419 @ (), record the violation information to drop@R_ 566_ 2419 @ in the system log directory (/ data / system)/ drop@R_566_2419 @), you can plug-in through the following command:

Permitcustomslowcalls(), permitdiskreads(), permitdiskwrites(), permitnetwork: if you want to turn off a detection, you can use the corresponding permit * method.

Vmpolicy details

The main methods of strictmode.vmpolicy.builder are as follows

Detectactivityleaks() user checks the memory leak of the activity

Memory leak check case:

When we rotate the screen repeatedly, we will output prompt information (focusing on the line instances = 2; limit = 1)

At this time, we created an anonymous inner class of thread in the activity, and the anonymous inner class implicitly holds the reference of the external class. Each time the screen is rotated, Android will create a new activity, and the original activity instance is held by the anonymous internal class thread we started, so it will not be released. From the log, there are four instances of the activity in the current system, but only one instance can be created. We keep flipping the screen, and the number of instances will continue to increase.

Detectleakedcloseableobjects() is used to remind when the resource is not closed properly

The following warnings are triggered after operation

Detect memory leak cases

The following warnings are triggered after operation

Other operations

In addition to viewing the log, we can also turn on the strict mode in the developer option. After it is turned on, if there is an operation that takes a long time in the main thread, the screen will flash. This is a more direct method.

matters needing attention

summary

The above is a detailed explanation of the use of Android strict mode introduced by Xiaobian. I hope it will be helpful to 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
分享
二维码
< <上一篇
下一篇>>