android - StrictMode.ThreadPolicy.Builder Purpose and Advantages? -
i referred strictmode.threadpolicy.builder
android document strictmode.threadpolicy.builder.
i have no clear idea strictmode.threadpolicy.builder
.
when have use class strictmode.threadpolicy.builder
.
what advantage , purpose of strictmode.threadpolicy.builder
.
i want detailed explanation for
strictmode.threadpolicy policy = new strictmode.threadpolicy.builder() .detectall() .penaltylog() .build(); strictmode.setthreadpolicy(policy);
the advantages of defining stictmode policies within application force you, in development phase, make application more behaved within device running on: avoid running consuming operation on ui thread, avoids activity leakages, , one. when define these in code, make application crashes if defined strict polices has been compromised, makes fixes issues you've done (the not behaved approaches, network operations on ui thread).
i following first when start 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 on main thread related * resource access. */ strictmode.setthreadpolicy(new strictmode.threadpolicy.builder() .detectall() .penaltylog() .penaltyflashscreen() .penaltydeath() .build()); /** * doesn't enable leakage of 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 androidmanifest.xml under application tag following:
android:debugable="true"
the following i've shown forces strictmode polices on application when it's in debug mode (the flag in manifest must removed before published it).
hope helps.
Comments
Post a Comment