IT. Expert System.

Android Reference

StrictMode


android.os

Class StrictMode



  • public final class StrictMode
    extends Object

    StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.

    StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application's main thread responsive, you also prevent ANR dialogs from being shown to users.

    Note that even though an Android device's disk is often on flash memory, many devices run a filesystem on top of that memory with very limited concurrency. It's often the case that almost all disk accesses are fast, but may in individual cases be dramatically slower when certain I/O is happening in the background from other processes. If possible, it's best to assume that such things are not fast.

    Example code to enable from early in your Application, Activity, or other application component's Application.onCreate() method:

     public void onCreate() {
         if (DEVELOPER_MODE) {
             StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                     .detectDiskReads()
                     .detectDiskWrites()
                     .detectNetwork()   // or .detectAll() for all detectable problems
                     .penaltyLog()
                     .build());
             StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                     .detectLeakedSqlLiteObjects()
                     .detectLeakedClosableObjects()
                     .penaltyLog()
                     .penaltyDeath()
                     .build());
         }
         super.onCreate();
     }
     

    You can decide what should happen when a violation is detected. For example, using StrictMode.ThreadPolicy.Builder.penaltyLog() you can watch the output of adb logcat while you use your application to see the violations as they happen.

    If you find violations that you feel are problematic, there are a variety of tools to help solve them: threads, Handler, AsyncTask, IntentService, etc. But don't feel compelled to fix everything that StrictMode finds. In particular, many cases of disk access are often necessary during the normal activity lifecycle. Use StrictMode to find things you did by accident. Network requests on the UI thread are almost always a problem, though.

    StrictMode is not a security mechanism and is not guaranteed to find all disk or network accesses. While it does propagate its state across process boundaries when doing Binder calls, it's still ultimately a best effort mechanism. Notably, disk or network access from JNI calls won't necessarily trigger it. Future versions of Android may catch more (or fewer) operations, so you should never leave StrictMode enabled in applications distributed on Google Play.

    • Field Detail

      • DISABLE_PROPERTY

        public static final String DISABLE_PROPERTY
        Boolean system property to disable strict mode checks outright. Set this to 'true' to force disable; 'false' has no effect on other enable/disable policy.
        See Also:
        Constant Field Values
      • VISUAL_PROPERTY

        public static final String VISUAL_PROPERTY
        The boolean system property to control screen flashes on violations.
        See Also:
        Constant Field Values
      • DETECT_CUSTOM

        public static final int DETECT_CUSTOM
        For StrictMode.noteSlowCall()
        See Also:
        Constant Field Values
      • DETECT_VM_CURSOR_LEAKS

        public static final int DETECT_VM_CURSOR_LEAKS
        Note, a "VM_" bit, not thread.
        See Also:
        Constant Field Values
      • DETECT_VM_CLOSABLE_LEAKS

        public static final int DETECT_VM_CLOSABLE_LEAKS
        Note, a "VM_" bit, not thread.
        See Also:
        Constant Field Values
      • DETECT_VM_ACTIVITY_LEAKS

        public static final int DETECT_VM_ACTIVITY_LEAKS
        Note, a "VM_" bit, not thread.
        See Also:
        Constant Field Values
      • DETECT_VM_REGISTRATION_LEAKS

        public static final int DETECT_VM_REGISTRATION_LEAKS
        See Also:
        Constant Field Values
      • PENALTY_DEATH

        public static final int PENALTY_DEATH
        Death on any detected violation.
        See Also:
        Constant Field Values
      • PENALTY_DEATH_ON_NETWORK

        public static final int PENALTY_DEATH_ON_NETWORK
        Death just for detected network usage.
        See Also:
        Constant Field Values
      • PENALTY_FLASH

        public static final int PENALTY_FLASH
        Flash the screen during violations.
        See Also:
        Constant Field Values
      • PENALTY_GATHER

        public static final int PENALTY_GATHER
        Non-public penalty mode which overrides all the other penalty bits and signals that we're in a Binder call and we should ignore the other penalty bits and instead serialize back all our offending stack traces to the caller to ultimately handle in the originating process. This must be kept in sync with the constant in libs/binder/Parcel.cpp
        See Also:
        Constant Field Values
    • Method Detail

      • setThreadPolicy

        public static void setThreadPolicy(StrictMode.ThreadPolicy policy)
        Sets the policy for what actions on the current thread should be detected, as well as the penalty if such actions occur.

        Internally this sets a thread-local variable which is propagated across cross-process IPC calls, meaning you can catch violations when a system service or another process accesses the disk or network on your behalf.

        Parameters:
        policy - the policy to put into place
      • getThreadPolicyMask

        public static int getThreadPolicyMask()
        Returns the bitmask of the current thread's policy.
        Returns:
        the bitmask of all the DETECT_* and PENALTY_* bits currently enabled
      • conditionallyEnableDebugLogging

        public static boolean conditionallyEnableDebugLogging()
        Enable DropBox logging for debug phone builds.
      • enableDeathOnNetwork

        public static void enableDeathOnNetwork()
        Used by the framework to make network usage on the main thread a fatal error.
      • conditionallyCheckInstanceCounts

        public static void conditionallyCheckInstanceCounts()
      • setVmPolicy

        public static void setVmPolicy(StrictMode.VmPolicy policy)
        Sets the policy for what actions in the VM process (on any thread) should be detected, as well as the penalty if such actions occur.
        Parameters:
        policy - the policy to put into place
      • vmSqliteObjectLeaksEnabled

        public static boolean vmSqliteObjectLeaksEnabled()
      • vmClosableObjectLeaksEnabled

        public static boolean vmClosableObjectLeaksEnabled()
      • vmRegistrationLeaksEnabled

        public static boolean vmRegistrationLeaksEnabled()
      • onSqliteObjectLeaked

        public static void onSqliteObjectLeaked(String message,
                                Throwable originStack)
      • onWebViewMethodCalledOnWrongThread

        public static void onWebViewMethodCalledOnWrongThread(Throwable originStack)
      • onIntentReceiverLeaked

        public static void onIntentReceiverLeaked(Throwable originStack)
      • onServiceConnectionLeaked

        public static void onServiceConnectionLeaked(Throwable originStack)
      • onVmPolicyViolation

        public static void onVmPolicyViolation(String message,
                               Throwable originStack)
      • enterCriticalSpan

        public static StrictMode.Span enterCriticalSpan(String name)
        Enter a named critical span (e.g. an animation)

        The name is an arbitary label (or tag) that will be applied to any strictmode violation that happens while this span is active. You must call finish() on the span when done.

        This will never return null, but on devices without debugging enabled, this may return a dummy object on which the finish() method is a no-op.

        TODO: add CloseGuard to this, verifying callers call finish.

      • noteDiskRead

        public static void noteDiskRead()
      • noteDiskWrite

        public static void noteDiskWrite()
      • trackActivity

        public static Object trackActivity(Object instance)
        Returns an object that is used to track instances of activites. The activity should store a reference to the tracker object in one of its fields.
      • incrementExpectedActivityCount

        public static void incrementExpectedActivityCount(Class klass)
      • decrementExpectedActivityCount

        public static void decrementExpectedActivityCount(Class klass)


Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 106 / 158775703. Delta: 0.02556 с