ANDROID_ Debugging development with Strictmode

Source: Internet
Author: User

this blog post for the original, reproduced please specify the source! http://blog.csdn.net/zimo2013/article/details/400760491.StrictMode Introduction

Since Android 2.3 provides a debug feature called strict mode (strictmode), Google says the feature has benefited Google apps on hundreds of Android. So what does it do? It will report policy violations related to threads and virtual machines. Once a policy violation has been detected, you will get a warning that it contains a stack trace showing where your app is violating the violation. You can force a warning instead of a crash (crash), or you can just count the warnings into the log for your app to continue executing.


2.ThreadPolicy Type

and thread-dependent, which is primarily for the main thread (or UI thread). Since it is not good to read and write disks and network access in the main thread, Google has added a strict mode (Strictmode) hook (hook) to the disk and network code. If you turn on strict mode (Strictmode) on a thread, you will get a warning when that thread is accessing the disk and network. You can choose the warning mode. Some of the violations contain user slow calls (custom slow calls so translate line?). ), disk read-write, network access. You can choose to write a warning to Logcat, display a dialog box, flash down the screen, write to the Dropbox log file, or let the app crash. The most common practice is to write logcat or let the app crash.

Check "Read/write disk", "Access Network" and "Show call" in thread

Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder ()  . Detectdiskreads ()  . Detectdiskwrites ()  . Detectnetwork ().  penaltylog ()  . Build ());


3.VmPolicy Type

There is also a virtual machine policy (vmpolicy) that can check for memory leaks, such as the end operation before closing a SQLite object, or any other similar closure of an object before it is closed. Virtual machine policy (Vmpolicy) is created by a similar builder class. Unlike thread policy (threadpolicy), Virtual machine policy (Vmpolicy) cannot provide a warning through a dialog box.

Check leaks for "cursor", "Close Method", "activity instance", "object instance" and "registration" in process

Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder ()     . Detectleakedsqlliteobjects ()     . Penaltylog ()     . Penaltydeath ()     . Build ());  


4. Usage examples

public class Uilapplication extends application {@TargetApi (build.version_codes. Gingerbread) @SuppressWarnings ("unused") @Overridepublic void OnCreate () {if (Developer_mode && Build.VERSION.SDK_INT >= build.version_codes. Gingerbread) {Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder (). Detectall (). Penaltydialog (). Build ()); Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder (). Detectall (). Penaltydeath (). build ());} Super.oncreate ();}}

ANDROID_ Debugging development with Strictmode

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.