Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of StrictMode strict Mode

Shulou Source: shulou.com Published: 2022-06-03 11:02:42 11月15日 Update

Editor to share with you the example analysis of StrictMode strict mode, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

StrictMode strict mode is a runtime detection mechanism provided by Android, which is generally used to detect some time-consuming actions in the main thread, such as IO read and write, database operations, Sp operations, Activity leakage, unclosed Closable object leakage, etc., in order to reduce the occurrence of ANR.

Analysis of strict Mode principle of StrictMode

Strict mode mainly has two strategies: one is thread policy, that is, TreadPolicy is for a specific thread, and the other is VM policy, that is, VmPolicy, which is for all objects of the virtual machine.

Of course, you can add Penalty (penalty), usually we are used to print logs, draw red boxes, and so on.

How to detect in StrictMode strict mode

Where is the code to inject StrictMode:

You can insert StrictMode check code directly into the source code, or you can implant code that is BlockGuard and CloseGuard.

Let's first take a look at UML:

2.png

BlockGuard:

PublicfinalclassStrictMode {

PublicstaticvoidsetThreadPolicy (finalThreadPolicypolicy) {

SetThreadPolicyMask (policy.mask)

}

PrivatestaticvoidsetThreadPolicyMask (finalintpolicyMask) {

SetBlockGuardPolicy (policyMask)

Binder.setThreadStrictModePolicy (policyMask)

}

/ / SetsthepolicyinDalvik/libcore (BlockGuard)

PrivatestaticvoidsetBlockGuardPolicy (finalintpolicyMask) {

If (policyMask==0) {

BlockGuard.setThreadPolicy (BlockGuard.LAX_POLICY)

Return

}

FinalBlockGuard.Policypolicy=BlockGuard.getThreadPolicy ()

FinalAndroidBlockGuardPolicyandroidPolicy

If (policyinstanceofAndroidBlockGuardPolicy) {

AndroidPolicy= (AndroidBlockGuardPolicy) policy

} else {

AndroidPolicy=threadAndroidPolicy.get ()

BlockGuard.setThreadPolicy (androidPolicy)

}

AndroidPolicy.setPolicyMask (policyMask)

}

}

Set the corresponding AndroidBlockGuardPolicy to the BlockGuard class in setBlockGuardPolicy (policyMask)

And Binder.setThreadStrictModePolicy () is the Native layer. When process An initiates a cross-process call into process B, the violation exception in process B will also be sent back to process A through Binder.

The above is all the contents of the article "sample Analysis of StrictMode strict Mode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Patterns code processes analysis policies articles threads detection examples content objects rigor not so much actions principles places most data databases logs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB OPPO Reno MySQL Huawei