# StrictMode

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary: [Nested Classes](https://developer.android.com/reference/android/os/StrictMode#nestedclasses) \| [Methods](https://developer.android.com/reference/android/os/StrictMode#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/os/StrictMode#inhmethods)  

# StrictMode

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/os/StrictMode "View this page in Kotlin") \|Java


`
public

final

class
StrictMode
`


`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


`

`


`

|---|-----------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.os.StrictMode |

<br />

*** ** * ** ***

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](https://developer.android.com/guide/practices/design/responsiveness) 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](https://developer.android.com/reference/android/app/Application), [Activity](https://developer.android.com/reference/android/app/Activity), or other application component's [Application.onCreate()](https://developer.android.com/reference/android/app/Application#onCreate())
method:

```
 override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     StrictMode.setThreadPolicy(
         StrictMode.ThreadPolicy.Builder()
         .detectAll()
         .build()
     )
     StrictMode.setVmPolicy(
         StrictMode.VmPolicy.Builder()
         .detectAll()
         .build()
     )
 }
 
```

You can decide what should happen when a violation is detected. For example, using [StrictMode.ThreadPolicy.Builder.penaltyLog()](https://developer.android.com/reference/android/os/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](https://developer.android.com/reference/android/os/Handler), [AsyncTask](https://developer.android.com/reference/android/os/AsyncTask), [IntentService](https://developer.android.com/reference/android/app/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](https://developer.android.com/reference/android/os/Binder) calls, it's still ultimately a best effort mechanism. Notably, disk or network
access from JNI calls won't necessarily trigger it.

## Summary

|                                                                                                                                                                                                                                                      ### Nested classes                                                                                                                                                                                                                                                      ||
|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` interface` | [StrictMode.OnThreadViolationListener](https://developer.android.com/reference/android/os/StrictMode.OnThreadViolationListener) When #[StrictMode.ThreadPolicy.Builder.penaltyListener(Executor, OnThreadViolationListener)](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy.Builder#penaltyListener(java.util.concurrent.Executor,%20android.os.StrictMode.OnThreadViolationListener)) is enabled, the listener is called on the provided executor when a Thread violation occurs. |
| ` interface` | [StrictMode.OnVmViolationListener](https://developer.android.com/reference/android/os/StrictMode.OnVmViolationListener) When #[StrictMode.VmPolicy.Builder.penaltyListener(Executor, OnVmViolationListener)](https://developer.android.com/reference/android/os/StrictMode.VmPolicy.Builder#penaltyListener(java.util.concurrent.Executor,%20android.os.StrictMode.OnVmViolationListener)) is enabled, the listener is called on the provided executor when a VM violation occurs.                             |
| ` class`     | [StrictMode.ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) [StrictMode](https://developer.android.com/reference/android/os/StrictMode) policy applied to a certain thread.                                                                                                                                                                                                                                                                                          |
| ` class`     | [StrictMode.VmPolicy](https://developer.android.com/reference/android/os/StrictMode.VmPolicy) [StrictMode](https://developer.android.com/reference/android/os/StrictMode) policy applied to all threads in the virtual machine's process.                                                                                                                                                                                                                                                                      |

|                                                                                                                                                                                                                                                                                                                                                                                         ### Public methods                                                                                                                                                                                                                                                                                                                                                                                         ||
|-----------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` static `[StrictMode.ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) | ` `[allowThreadDiskReads](https://developer.android.com/reference/android/os/StrictMode#allowThreadDiskReads())`() ` A convenience wrapper that takes the current [ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) from [getThreadPolicy()](https://developer.android.com/reference/android/os/StrictMode#getThreadPolicy()), modifies it to permit disk reads, and sets the new policy with [setThreadPolicy(ThreadPolicy)](https://developer.android.com/reference/android/os/StrictMode#setThreadPolicy(android.os.StrictMode.ThreadPolicy)), returning the old policy so you can restore it at the end of a block.                  |
| ` static `[StrictMode.ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) | ` `[allowThreadDiskWrites](https://developer.android.com/reference/android/os/StrictMode#allowThreadDiskWrites())`() ` A convenience wrapper that takes the current [ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) from [getThreadPolicy()](https://developer.android.com/reference/android/os/StrictMode#getThreadPolicy()), modifies it to permit both disk reads \& writes, and sets the new policy with [setThreadPolicy(ThreadPolicy)](https://developer.android.com/reference/android/os/StrictMode#setThreadPolicy(android.os.StrictMode.ThreadPolicy)), returning the old policy so you can restore it at the end of a block. |
| ` static void`                                                                                                  | ` `[enableDefaults](https://developer.android.com/reference/android/os/StrictMode#enableDefaults())`() ` Enables the recommended StrictMode defaults, with violations just being logged.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ` static `[StrictMode.ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) | ` `[getThreadPolicy](https://developer.android.com/reference/android/os/StrictMode#getThreadPolicy())`() ` Returns the current thread's policy.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ` static `[StrictMode.VmPolicy](https://developer.android.com/reference/android/os/StrictMode.VmPolicy)         | ` `[getVmPolicy](https://developer.android.com/reference/android/os/StrictMode#getVmPolicy())`() ` Gets the current VM policy.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ` static void`                                                                                                  | ` `[noteSlowCall](https://developer.android.com/reference/android/os/StrictMode#noteSlowCall(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` name) ` For code to note that it's slow.                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ` static void`                                                                                                  | ` `[setThreadPolicy](https://developer.android.com/reference/android/os/StrictMode#setThreadPolicy(android.os.StrictMode.ThreadPolicy))`(`[StrictMode.ThreadPolicy](https://developer.android.com/reference/android/os/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.                                                                                                                                                                                                                                                                                               |
| ` static void`                                                                                                  | ` `[setVmPolicy](https://developer.android.com/reference/android/os/StrictMode#setVmPolicy(android.os.StrictMode.VmPolicy))`(`[StrictMode.VmPolicy](https://developer.android.com/reference/android/os/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.                                                                                                                                                                                                                                                                                                       |

| ### Inherited methods |
|-----------------------|---|
| From class ` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                        | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                             | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                            | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this `Object`.                                                                                                                                                 | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                   | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                              | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                           | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                              | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.                                                                                             | ||

## Public methods

### allowThreadDiskReads

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static StrictMode.ThreadPolicy allowThreadDiskReads ()
```

A convenience wrapper that takes the current [ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) from [getThreadPolicy()](https://developer.android.com/reference/android/os/StrictMode#getThreadPolicy()), modifies it to permit disk reads, and sets the new policy with [setThreadPolicy(ThreadPolicy)](https://developer.android.com/reference/android/os/StrictMode#setThreadPolicy(android.os.StrictMode.ThreadPolicy)), returning the old policy so you can restore it at the end of a block.

<br />

|                                                                                       Returns                                                                                        ||
|-------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|
| [StrictMode.ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) | the old policy, to be passed to setThreadPolicy to restore the policy. <br /> |

### allowThreadDiskWrites

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static StrictMode.ThreadPolicy allowThreadDiskWrites ()
```

A convenience wrapper that takes the current [ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) from [getThreadPolicy()](https://developer.android.com/reference/android/os/StrictMode#getThreadPolicy()), modifies it to permit both disk reads \& writes, and sets the new
policy with [setThreadPolicy(ThreadPolicy)](https://developer.android.com/reference/android/os/StrictMode#setThreadPolicy(android.os.StrictMode.ThreadPolicy)), returning the old policy so you can restore it at the
end of a block.

<br />

|                                                                                                                                                                   Returns                                                                                                                                                                    ||
|-------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [StrictMode.ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) | the old policy, to be passed to [setThreadPolicy(ThreadPolicy)](https://developer.android.com/reference/android/os/StrictMode#setThreadPolicy(android.os.StrictMode.ThreadPolicy)) to restore the policy at the end of a block <br /> |

### enableDefaults

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static void enableDefaults ()
```

Enables the recommended StrictMode defaults, with violations just being logged.

This catches disk and network access on the main thread, as well as leaked SQLite cursors
and unclosed resources. This is simply a wrapper around [setVmPolicy(VmPolicy)](https://developer.android.com/reference/android/os/StrictMode#setVmPolicy(android.os.StrictMode.VmPolicy)) and [setThreadPolicy(ThreadPolicy)](https://developer.android.com/reference/android/os/StrictMode#setThreadPolicy(android.os.StrictMode.ThreadPolicy)).

<br />

### getThreadPolicy

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static StrictMode.ThreadPolicy getThreadPolicy ()
```

Returns the current thread's policy.

<br />

|                                                    Returns                                                    ||
|-------------------------------------------------------------------------------------------------------|--------|
| [StrictMode.ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) | <br /> |

### getVmPolicy

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static StrictMode.VmPolicy getVmPolicy ()
```

Gets the current VM policy.

<br />

|                                                Returns                                                ||
|-----------------------------------------------------------------------------------------------|--------|
| [StrictMode.VmPolicy](https://developer.android.com/reference/android/os/StrictMode.VmPolicy) | <br /> |

### noteSlowCall

Added in [API level 11](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static void noteSlowCall (String name)
```

For code to note that it's slow. This is a no-op unless the current thread's [StrictMode.ThreadPolicy](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy) has [StrictMode.ThreadPolicy.Builder.detectCustomSlowCalls()](https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls()) enabled.

<br />

|                                               Parameters                                               ||
|--------|------------------------------------------------------------------------------------------------|
| `name` | `String`: a short string for the exception stack trace that's built if when this fires. <br /> |

### setThreadPolicy

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                                Parameters                                ||
|----------|----------------------------------------------------------------|
| `policy` | `StrictMode.ThreadPolicy`: the policy to put into place <br /> |

### setVmPolicy

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                              Parameters                              ||
|----------|------------------------------------------------------------|
| `policy` | `StrictMode.VmPolicy`: the policy to put into place <br /> |