The Android Thread Police are coming….


As part of the Gingerbread release of android, a new API, StrictMode will allow developers to set a policy on a thread.

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.

StrictMode will only come into play if you select it and allows you to detect the following situations:

  • detect disk writes
  • detect disk reads
  • detect network usage
  • on a violation: log
  • on a violation: crash
  • on a violation: dropbox
  • on a violation: show an annoying dialog

Enabling StrictMode

StrictMode can be enabled in your applications onCreate method

public void onCreate() {
     if (DEVELOPER_MODE) {
         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                 .detectDiskReads()
                 .detectDiskWrites()
                 .detectNetwork()
                 .penaltyLog()
                 .build());
     }
     super.onCreate();
 }

This blog post on the Android Developers blog by Brad Fitzpatrick will give you an overview of what you can expect. You can also check out the StrictMode API here for more details on what it allows.

, , , ,

  1. No comments yet.
(will not be published)