StrictMode

class StrictMode
kotlin.Any
   ↳ android.os.StrictMode

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 android.app.Application, , or other application component's android.app.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 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, android.os.Handler, android.os.AsyncTask, , 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 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.
Requires API level 9 (Android 2.3, Gingerbread)

Summary

Nested classes
abstract

When #ThreadPolicy.Builder#penaltyListener is enabled, the listener is called on the provided executor when a Thread violation occurs.

abstract

When #VmPolicy.Builder#penaltyListener is enabled, the listener is called on the provided executor when a VM violation occurs.

StrictMode policy applied to a certain thread.

StrictMode policy applied to all threads in the virtual machine's process.

Public methods
static Unit

Sets the policy for what actions on the current thread should be detected, as well as the penalty if such actions occur.

static StrictMode.ThreadPolicy!

Returns the current thread's policy.

static StrictMode.ThreadPolicy!

A convenience wrapper that takes the current ThreadPolicy from , modifies it to permit both disk reads & writes, and sets the new policy with #setThreadPolicy, returning the old policy so you can restore it at the end of a block.

static StrictMode.ThreadPolicy!

A convenience wrapper that takes the current ThreadPolicy from , modifies it to permit disk reads, and sets the new policy with , returning the old policy so you can restore it at the end of a block.

static Unit

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.

static StrictMode.VmPolicy!

Gets the current VM policy.

static Unit

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

static Unit

For code to note that it's slow.

Public methods

setThreadPolicy

added in API level 9
static fun setThreadPolicy(policy: StrictMode.ThreadPolicy!): Unit

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.
Requires API level 9 (Android 2.3, Gingerbread)

Parameters
policy StrictMode.ThreadPolicy!: the policy to put into place

getThreadPolicy

added in API level 9
static fun getThreadPolicy(): StrictMode.ThreadPolicy!

Returns the current thread's policy.
Requires API level 9 (Android 2.3, Gingerbread)

allowThreadDiskWrites

added in API level 9
static fun allowThreadDiskWrites(): StrictMode.ThreadPolicy!

A convenience wrapper that takes the current ThreadPolicy from , modifies it to permit both disk reads & writes, and sets the new policy with #setThreadPolicy, returning the old policy so you can restore it at the end of a block.
Requires API level 9 (Android 2.3, Gingerbread)

Return
StrictMode.ThreadPolicy!: the old policy, to be passed to #setThreadPolicy to restore the policy at the end of a block

allowThreadDiskReads

added in API level 9
static fun allowThreadDiskReads(): StrictMode.ThreadPolicy!

A convenience wrapper that takes the current ThreadPolicy from , modifies it to permit disk reads, and sets the new policy with , returning the old policy so you can restore it at the end of a block.
Requires API level 9 (Android 2.3, Gingerbread)

Return
StrictMode.ThreadPolicy!: the old policy, to be passed to setThreadPolicy to restore the policy.

setVmPolicy

added in API level 9
static fun setVmPolicy(policy: StrictMode.VmPolicy!): Unit

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.
Requires API level 9 (Android 2.3, Gingerbread)

Parameters
policy StrictMode.VmPolicy!: the policy to put into place

getVmPolicy

added in API level 9
static fun getVmPolicy(): StrictMode.VmPolicy!

Gets the current VM policy.
Requires API level 9 (Android 2.3, Gingerbread)

enableDefaults

added in API level 9
static fun enableDefaults(): Unit

Enable 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 and .
Requires API level 9 (Android 2.3, Gingerbread)

noteSlowCall

added in API level 11
static fun noteSlowCall(name: String!): Unit

For code to note that it's slow. This is a no-op unless the current thread's has enabled.
Requires API level 11 (Android 3.0, Honeycomb)

Parameters
name String!: a short string for the exception stack trace that's built if when this fires.