Android Strictmode Introduction

Source: Internet
Author: User

Ext.: http://www.blueowls.net/android-strictmode%E4%BB%8B%E7%BB%8D/

/** * Enables "strict mode" for Testing-should never is used in release builds */@TargetApi (Build.version_co Des. Jelly_bean) private static void Enablestrictmode () {//return if the build is not a debug build if (!            Buildconfig.debug) {APPLOG.E (t.utils, "You should not call Enablestrictmode () on a non DEBUG build");        Return                } strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder (). Detectdiskreads ()                . Detectdiskwrites (). Detectnetwork (). Penaltylog (). Penaltyflashscreen ()        . build ()); Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder (). Detectactivityleaks (). Detectleake Dsqlliteobjects (). Detectleakedclosableobjects (). Detectleakedregistrationobjects ()//<-        -Requires Jelly Bean. Penaltylog (). build ()); APPLOG.W (T.utILS, "Strict mode Enabled"); }

  

Android platform (from Android 2.3), new added a new class, called Strictmode (Android.os.StrictMode). This class can be used to help developers improve the applications they write and to provide a variety of strategies that can check and report on problems in developer applications at any time, such as monitoring work that should not have been done in the main thread or some other nonstandard and bad code.
Strictmode has a number of different strategies, each with different rules, and when a developer violates a rule, each strategy has a different way of showing the user. In this article, we'll give an example of how to use Strictmode in Android.
Strictmode's policies and rules
Currently, there are two major types of strategies to use, one for common monitoring, and the other for VM virtual machines. Common monitoring strategies include the following:
Disk Reads disks Read
Disk writes diskette write
Network access
Custom Slow code customization for slow-running codes analysis
The first three kinds of meaning readers should be very clear, just as their name shows, respectively, the disk read and write, network access monitoring. The fourth type of custom slow code analysis, which is triggered only when the calling class is accessed, can be used to monitor slow-running code. When called in the main thread, these validation rules will work to check your code. For example, when your application is downloading or parsing a large amount of data, you can trigger a custom run slow code query analysis, a great role. Strictmode can be used to capture time-consuming disk, network access, or function calls that occur in the main thread of the application, helping developers to improve their programs, making the main thread processing UI and animations smoother when disk reads and writes and network operations, avoiding blocking of the main thread.
The VM strategy focuses on the following categories:
Memory-Leaking Activity object
SQLite objects with memory leaks
Freed objects with memory leaks
Among them, memory leaks of activity objects and memory leaks of SQLite objects are better understood, and so-called to close the object of the check, mainly to monitor those who should be freed, such as the object should call the close () method.
When a developer violates a certain type of rule, each strategy has a different way of making the developer aware of the situation. Related violations can be recorded in Logcat or stored in the DropBox (android.os.DropBox) service. The common monitoring class strategy also shows the relevant dialog box and the context when the violation occurs, all in order to allow developers to quickly understand the flaws of the program to submit the quality of the program.
First step Enable Strictmode
In order to enable and configure Strictmode in the application, it is best to use the early segment of the application's life cycle as much as possible, by calling the Strictmode method Setthreadpolicy. When using common monitoring classes, one of the best times to call is when the entry and activities are invoked before the application. For example, in an application, you can put the code in the OnCreate () method that launches the activity class, below is a code example that enables all policies and rules in the current situation, and when a program violates a commonly used rule, the relevant prompt window appears:

Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder ().         Detectall ().         penaltylog ()         . Penaltydialog ()////print Logcat, of course, you can also navigate to Dropbox and save the corresponding log        . Build () from the file; Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder (). Detectall ()         . Penaltylog ()         . Build ());

  

Of course, the above code should only be run in an app that is not released on-line beta, to facilitate monitoring of related operations, and Strictmode should not be enabled on the production version. Therefore, the best code practice should look like this:

public void OnCreate () {      if (developer_mode) {          Strictmode.setthreadpolicy (new StrictMode.ThreadPolicy.Builder ().                  detectdiskreads ().                  detectdiskwrites ().                  detectnetwork                  (). Penaltylog ()                  . Build ());      }      Super.oncreate ();  }  

  

public void OnCreate () {      if (developer_mode) {          Strictmode.setthreadpolicy (new StrictMode.ThreadPolicy.Builder ().                  detectdiskreads ().                  detectdiskwrites ().                  detectnetwork                  (). Penaltylog ()                  . Build ());      }      Super.oncreate ();  }  Here, you perform some quick read and write operations to the disk, and finally re-enable the rules.  Strictmode.threadpolicy old = Strictmode.getthreadpolicy ();  Strictmode.setthreadpolicy (new StrictMode.ThreadPolicy.Builder (old). Permitdiskwrites (). build ());  Docorrectstuffthatwritestodisk (); Strictmode.setthreadpolicy (old);

  

Strictmode is a very useful class that can be easily applied to check the performance and problems of Android applications. When this mode is turned on, the developer can check the potential problems in the application very well, and refer to the relevant API instructions in the Android documentation.

Android Strictmode Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.