7

I referred StrictMode.ThreadPolicy.Builder from android document StrictMode.ThreadPolicy.Builder.

I have no clear idea about StrictMode.ThreadPolicy.Builder.
When we have to use this class StrictMode.ThreadPolicy.Builder.
What is the advantage and purpose of StrictMode.ThreadPolicy.Builder.
I want detailed explanation for

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
 .detectAll()
 .penaltyLog()
 .build();
StrictMode.setThreadPolicy(policy);
Jumpo
  • 641
  • 2
  • 6
  • 7
  • Did you read [the blog post on `StrictMode`](http://android-developers.blogspot.co.nz/2010/12/new-gingerbread-api-strictmode.html)? – MH. Aug 07 '13 at 10:03

1 Answers1

8

The advantages of defining stictmode policies within your application is to force you in the development phase to make your application more well behaved within the device it is running on: avoid running IO operations on the UI thread, avoids Activity leakages, and so on. When you define these policies in your code, you make your application crashes if the defined strict polices has been compromised, which makes you fixes the issues you've done (the not well behaved approaches, like network operations on the UI thread).

I like to do the following for first when I start a new project:

public class MyApplication extends Application {

private static final String TAG = "MyApplication";

@Override
public void onCreate() {
    if (BuildConfig.DEBUG) {
        Log.w(TAG, "======================================================");
        Log.w(TAG, "======= APPLICATION IN STRICT MODE - DEBUGGING =======");
        Log.w(TAG, "======================================================");

        /**
         * Doesn't enable anything on the main thread that related
         * to resource access.
         */
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .penaltyFlashScreen()
                .penaltyDeath()
                .build());

        /**
         * Doesn't enable any leakage of the application's components.
         */
        final StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder.detectLeakedRegistrationObjects();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            builder.detectFileUriExposure();
        }
        builder.detectLeakedClosableObjects()
               .detectLeakedSqlLiteObjects()
               .penaltyLog()
               .penaltyDeath();
        StrictMode.setVmPolicy(builder.build());
    }
    super.onCreate();
}

}

And setting the AndroidManifest.xml under the application tag the following:

android:debuggable="true"

The following I've shown you forces Strictmode polices on my application only when it's in debug mode (the flag in the manifest must be removed before you published it).

Hope it helps.

Graziano
  • 313
  • 1
  • 4
  • 11