StrictMode.ThreadPolicy.Builder的目的和优势? [英] StrictMode.ThreadPolicy.Builder Purpose and Advantages?

查看:131
本文介绍了StrictMode.ThreadPolicy.Builder的目的和优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Android文档StrictMode.ThreadPolicy.Builder ="nofollow"> StrictMode.ThreadPolicy.Builder .

我对 StrictMode.ThreadPolicy.Builder 不清楚.
当我们必须使用此类 StrictMode.ThreadPolicy.Builder 时.
StrictMode.ThreadPolicy.Builder 的优点和目的是什么?
我想要详细的解释

  StrictMode.ThreadPolicy策略=新的StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().建造();StrictMode.setThreadPolicy(policy); 

解决方案

在应用程序中定义stictmode策略的优点是迫使您进入开发阶段,以使您的应用程序在其所运行的设备中表现得更好:在UI线程上运行IO操作,避免Activity泄漏等.在代码中定义这些代码时,如果已定义的严格策略遭到破坏,就会使应用程序崩溃,从而使您可以解决已解决的问题(行为不佳的方法,例如UI线程上的网络操作).

当我开始一个新项目时,我喜欢先做以下事情:

 公共类MyApplication扩展了Application {私有静态最终String TAG ="MyApplication";@Override公共无效onCreate(){如果(BuildConfig.DEBUG){Log.w(TAG,"=======================================================));Log.w(TAG,"=======严格模式下的应用-调试=======");Log.w(TAG,"=======================================================));/***不会在主线程上启用任何与之相关的功能*资源访问.*/StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().penaltyFlashScreen().penaltyDeath().建造());/***不会导致应用程序组件的任何泄漏.*/最终的StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.JELLY_BEAN){builder.detectLeakedRegistrationObjects();}如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.JELLY_BEAN_MR2){builder.detectFileUriExposure();}builder.detectLeakedClosableObjects().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath();StrictMode.setVmPolicy(builder.build());}super.onCreate();} 

}

并在应用程序标签下设置AndroidManifest.xml如下:

  android:debugable ="true" 

以下我向您展示的是,仅当应用程序处于调试模式时,才将Strictmode策略强制应用到我的应用程序上(清单中的标志必须在发布之前删除).

希望有帮助.

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);

解决方案

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 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:debugable="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.

这篇关于StrictMode.ThreadPolicy.Builder的目的和优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆