1. StrictMode is a class introduced by android API 9, which helps developers find some problems in code. It can set some policies. When a program violates these policies, it will prompt developers. There are two main types of policies: thread-based and VM-based. In terms of thread policy, it can discover code with slow write speed when the UI thread reads and writes DISK, the UI thread accesses the network. In terms of VM policy, it can detect Activity memory leaks, SQL object memory leaks, and resources are not released, which can limit the maximum number of objects in a class. When a program violates these policies, it notifies developers, such as log, in some ways. The Code is as follows.
Public static void enableStrictMode () {if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.GINGERBREAD) {StrictMode. threadPolicy. builder threadPolicyBuilder = // thread policy new StrictMode. threadPolicy. builder (). detectAll () // detects all rule violations. penaltyLog (); // if a policy violation is detected, print the log. For more information, see API StrictMode. vmPolicy. builder vmPolicyBuilder = // VM policy new StrictMode. vmPolicy. builder (). detectAll (). penaltyLog (); if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.HONEYCOMB) {threadPolicyBuilder. penaltyFlashScreen (); // detects a violation of the policy, which is a flickering vmPolicyBuilder. setClassInstanceLimit (MyClass. class, 1); // only one instance of MyClass can be restricted here; otherwise, the policy is violated.} StrictMode. setThreadPolicy (threadPolicyBuilder. build (); StrictMode. setVmPolicy (vmPolicyBuilder. build ());}}
You can put it in Application. onCreate (), so that when the program is opened, it will take effect. Do not use it in the release version, which consumes a lot of performance.
if(debug){ <pre name="code" class="java"><span style="white-space:pre"> </span>enableStrictMode() ;</pre>} <pre></pre> <br> <br>