Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of StrictMode strict Mode

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report