Android Guides | Samples

Android.OS.StrictMode Class

See Also: StrictMode

Syntax

[Android.Runtime.Register("android/os/StrictMode", DoNotGenerateAcw=true)]
public sealed class StrictMode : Object

Remarks

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:

java Example

 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 NoType:android/os/StrictMode$ThreadPolicy$Builder;Href=../../../reference/android/os/StrictMode.ThreadPolicy.Builder.html#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`3, NoType:android/app/IntentService;Href=../../../reference/android/app/IntentService.html, 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.

[Android Documentation]

Requirements

Namespace: Android.OS
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 9

The members of Android.OS.StrictMode are listed below.

See Also: Object

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

static
AllowThreadDiskReads() : StrictMode+ThreadPolicy
A convenience wrapper that takes the current NoType:android/os/StrictMode$ThreadPolicy;Href=../../../reference/android/os/StrictMode.ThreadPolicy.html from StrictMode.GetThreadPolicy, modifies it to permit disk reads, and sets the new policy with StrictMode.SetThreadPolicy(.ThreadPolicy), returning the old policy so you can restore it at the end of a block.
static
AllowThreadDiskWrites() : StrictMode+ThreadPolicy
A convenience wrapper that takes the current NoType:android/os/StrictMode$ThreadPolicy;Href=../../../reference/android/os/StrictMode.ThreadPolicy.html from StrictMode.GetThreadPolicy, modifies it to permit both disk reads & writes, and sets the new policy with StrictMode.SetThreadPolicy(.ThreadPolicy), returning the old policy so you can restore it at the end of a block.
static
EnableDefaults()
Enable the recommended StrictMode defaults, with violations just being logged.
static
GetThreadPolicy() : StrictMode+ThreadPolicy
Returns the current thread's policy.
static
GetVmPolicy() : StrictMode+VmPolicy
Gets the current VM policy.
static
NoteSlowCall(String)
For code to note that it's slow.
static
SetThreadPolicy(StrictMode+ThreadPolicy)
Sets the policy for what actions on the current thread should be detected, as well as the penalty if such actions occur.
static
SetVmPolicy(StrictMode+VmPolicy)
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.