StrictMode.ThreadPolicy.Builder


public static final class StrictMode.ThreadPolicy.Builder
extends Object

java.lang.Object
   ↳ android.os.StrictMode.ThreadPolicy.Builder


Creates ThreadPolicy instances. Methods whose names start with detect specify what problems we should look for. Methods whose names start with penalty specify what we should do when we detect a problem.

You can call as many detect and penalty methods as you like. Currently order is insignificant: all penalties apply to all detected problems.

For example, detect everything and log anything that's found:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
    .detectAll()
    .penaltyLog()
    .build();
StrictMode.setThreadPolicy(policy);

Summary

Public constructors

Builder()

Creates a Builder that detects nothing and has no violations.

Builder(StrictMode.ThreadPolicy policy)

Initializes a Builder from an existing ThreadPolicy.

Public methods

StrictMode.ThreadPolicy build()

Constructs the ThreadPolicy instance.

StrictMode.ThreadPolicy.Builder detectAll()

Detects everything that's potentially suspect.

StrictMode.ThreadPolicy.Builder detectCustomSlowCalls()

Enables detection of slow calls.

StrictMode.ThreadPolicy.Builder detectDiskReads()

Enables detection of disk reads.

StrictMode.ThreadPolicy.Builder detectDiskWrites()

Enables detection of disk writes.

StrictMode.ThreadPolicy.Builder detectExplicitGc()

Detects calls to Runtime.gc().

StrictMode.ThreadPolicy.Builder detectNetwork()

Enables detection of network operations.

StrictMode.ThreadPolicy.Builder detectResourceMismatches()

Enables detection of mismatches between defined resource types and getter calls.

StrictMode.ThreadPolicy.Builder detectUnbufferedIo()

Detects unbuffered input/output operations.

StrictMode.ThreadPolicy.Builder penaltyDeath()

Crashes the whole process on violation.

StrictMode.ThreadPolicy.Builder penaltyDeathOnNetwork()

Crashes the whole process on any network usage.

StrictMode.ThreadPolicy.Builder penaltyDialog()

Shows an annoying dialog to the developer on detected violations, rate-limited to be only a little annoying.

StrictMode.ThreadPolicy.Builder penaltyDropBox()

Enables detected violations log a stacktrace and timing data to the DropBox on policy violation.

StrictMode.ThreadPolicy.Builder penaltyFlashScreen()

Flashes the screen during a violation.

StrictMode.ThreadPolicy.Builder penaltyListener(Executor executor, StrictMode.OnThreadViolationListener listener)

Calls #OnThreadViolationListener.onThreadViolation(Violation) on specified executor every violation.

StrictMode.ThreadPolicy.Builder penaltyLog()

Logs detected violations to the system log.

StrictMode.ThreadPolicy.Builder permitAll()

Disables the detection of everything.

StrictMode.ThreadPolicy.Builder permitCustomSlowCalls()

Disables detection of slow calls.

StrictMode.ThreadPolicy.Builder permitDiskReads()

Disables detection of disk reads.

StrictMode.ThreadPolicy.Builder permitDiskWrites()

Disables detection of disk writes.

StrictMode.ThreadPolicy.Builder permitExplicitGc()

Disables detection of calls to Runtime.gc().

StrictMode.ThreadPolicy.Builder permitNetwork()

Disables detection of network operations.

StrictMode.ThreadPolicy.Builder permitResourceMismatches()

Disables detection of mismatches between defined resource types and getter calls.

StrictMode.ThreadPolicy.Builder permitUnbufferedIo()

Disables detection of unbuffered input/output operations.

Inherited methods

Public constructors

Builder

public Builder ()

Creates a Builder that detects nothing and has no violations. (but note that build() will default to enabling penaltyLog() if no other penalties are specified)

Builder

public Builder (StrictMode.ThreadPolicy policy)

Initializes a Builder from an existing ThreadPolicy.

Parameters
policy StrictMode.ThreadPolicy

Public methods

build

public StrictMode.ThreadPolicy build ()

Constructs the ThreadPolicy instance.

Note: if no penalties are enabled before calling build, penaltyLog() is implicitly set.

Returns
StrictMode.ThreadPolicy

detectAll

public StrictMode.ThreadPolicy.Builder detectAll ()

Detects everything that's potentially suspect.

As of the Gingerbread release this includes network and disk operations but will likely expand in future releases.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

detectCustomSlowCalls

public StrictMode.ThreadPolicy.Builder detectCustomSlowCalls ()

Enables detection of slow calls.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

detectDiskReads

public StrictMode.ThreadPolicy.Builder detectDiskReads ()

Enables detection of disk reads.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

detectDiskWrites

public StrictMode.ThreadPolicy.Builder detectDiskWrites ()

Enables detection of disk writes.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

detectExplicitGc

public StrictMode.ThreadPolicy.Builder detectExplicitGc ()

Detects calls to Runtime.gc().

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

detectNetwork

public StrictMode.ThreadPolicy.Builder detectNetwork ()

Enables detection of network operations.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

detectResourceMismatches

public StrictMode.ThreadPolicy.Builder detectResourceMismatches ()

Enables detection of mismatches between defined resource types and getter calls.

This helps detect accidental type mismatches and potentially expensive type conversions when obtaining typed resources.

For example, a strict mode violation would be thrown when calling TypedArray.getInt(int, int) on an index that contains a String-type resource. If the string value can be parsed as an integer, this method call will return a value without crashing; however, the developer should format the resource as an integer to avoid unnecessary type conversion.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

detectUnbufferedIo

public StrictMode.ThreadPolicy.Builder detectUnbufferedIo ()

Detects unbuffered input/output operations.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

penaltyDeath

public StrictMode.ThreadPolicy.Builder penaltyDeath ()

Crashes the whole process on violation. This penalty runs at the end of all enabled penalties so you'll still get see logging or other violations before the process dies.

Unlike penaltyDeathOnNetwork(), this applies to disk reads, disk writes, and network usage if their corresponding detect flags are set.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

penaltyDeathOnNetwork

public StrictMode.ThreadPolicy.Builder penaltyDeathOnNetwork ()

Crashes the whole process on any network usage. Unlike penaltyDeath(), this penalty runs before anything else. You must still have called detectNetwork() to enable this.

In the Honeycomb or later SDKs, this is on by default.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

penaltyDialog

public StrictMode.ThreadPolicy.Builder penaltyDialog ()

Shows an annoying dialog to the developer on detected violations, rate-limited to be only a little annoying.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

penaltyDropBox

public StrictMode.ThreadPolicy.Builder penaltyDropBox ()

Enables detected violations log a stacktrace and timing data to the DropBox on policy violation. Intended mostly for platform integrators doing beta user field data collection.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

penaltyFlashScreen

public StrictMode.ThreadPolicy.Builder penaltyFlashScreen ()

Flashes the screen during a violation.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

penaltyListener

public StrictMode.ThreadPolicy.Builder penaltyListener (Executor executor, 
                StrictMode.OnThreadViolationListener listener)

Calls #OnThreadViolationListener.onThreadViolation(Violation) on specified executor every violation.

Parameters
executor Executor: This value cannot be null.

listener StrictMode.OnThreadViolationListener: This value cannot be null.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

penaltyLog

public StrictMode.ThreadPolicy.Builder penaltyLog ()

Logs detected violations to the system log.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

permitAll

public StrictMode.ThreadPolicy.Builder permitAll ()

Disables the detection of everything.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

permitCustomSlowCalls

public StrictMode.ThreadPolicy.Builder permitCustomSlowCalls ()

Disables detection of slow calls.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

permitDiskReads

public StrictMode.ThreadPolicy.Builder permitDiskReads ()

Disables detection of disk reads.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

permitDiskWrites

public StrictMode.ThreadPolicy.Builder permitDiskWrites ()

Disables detection of disk writes.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

permitExplicitGc

public StrictMode.ThreadPolicy.Builder permitExplicitGc ()

Disables detection of calls to Runtime.gc().

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

permitNetwork

public StrictMode.ThreadPolicy.Builder permitNetwork ()

Disables detection of network operations.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

permitResourceMismatches

public StrictMode.ThreadPolicy.Builder permitResourceMismatches ()

Disables detection of mismatches between defined resource types and getter calls.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.

permitUnbufferedIo

public StrictMode.ThreadPolicy.Builder permitUnbufferedIo ()

Disables detection of unbuffered input/output operations.

Returns
StrictMode.ThreadPolicy.Builder This value cannot be null.