Adview loadAd()触发StrictMode违规

时间:2016-08-13 08:19:18

标签: android adview android-strictmode

我正在尝试在我的应用程序中添加一些添加内容。当我启用了StrictMode时,我得到了一堆像这样的策略违规(可能是6或7)。这是正常的吗?

18817-18817/com.xyz.xyz.xyz D/StrictMode: StrictMode policy violation; ~duration=67937 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=5439503 violation=2
                                                   at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1263)
                                                   at libcore.io.BlockGuardOs.fstat(BlockGuardOs.java:132)
                                                   at java.io.RandomAccessFile.length(RandomAccessFile.java:240)
                                                   at java.util.zip.ZipFile.readCentralDir(ZipFile.java:386)
                                                   at java.util.zip.ZipFile.<init>(ZipFile.java:175)
                                                   at java.util.zip.ZipFile.<init>(ZipFile.java:142)
                                                   at android.webkit.WebViewFactory.getLoadFromApkPath(WebViewFactory.java:357)
                                                   at android.webkit.WebViewFactory.getWebViewNativeLibraryPaths(WebViewFactory.java:407)
                                                   at android.webkit.WebViewFactory.loadNativeLibrary(WebViewFactory.java:511)
                                                   at android.webkit.WebViewFactory.getProviderClass(WebViewFactory.java:188)
                                                   at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:158)
                                                   at android.webkit.WebSettings.getDefaultUserAgent(WebSettings.java:1239)
                                                   at com.google.android.gms.ads.internal.util.ai.a(:com.google.android.gms.DynamiteModulesA:414)
                                                   at com.google.android.gms.ads.internal.util.y.a(:com.google.android.gms.DynamiteModulesA:419)
                                                   at com.google.android.gms.ads.internal.state.h.a(:com.google.android.gms.DynamiteModulesA:349)
                                                   at com.google.android.gms.ads.internal.a.<init>(:com.google.android.gms.DynamiteModulesA:153)
                                                   at com.google.android.gms.ads.internal.c.<init>(:com.google.android.gms.DynamiteModulesA:104)
                                                   at com.google.android.gms.ads.internal.c.<init>(:com.google.android.gms.DynamiteModulesA:92)
                                                   at com.google.android.gms.ads.internal.g.<init>(:com.google.android.gms.DynamiteModulesA:55)
                                                   at com.google.android.gms.ads.internal.o.<init>(:com.google.android.gms.DynamiteModulesA:89)
                                                   at com.google.android.gms.ads.ChimeraAdManagerCreatorImpl.newAdManagerByType(:com.google.android.gms.DynamiteModulesA:66)
                                                   at com.google.android.gms.ads.internal.client.al.onTransact(:com.google.android.gms.DynamiteModulesA:91)
                                                   at android.os.Binder.transact(Binder.java:387)
                                                   at com.google.android.gms.ads.internal.client.am.newAdManagerByType(:com.google.android.gms:170)
                                                   at com.google.android.gms.ads.AdManagerCreatorImpl.newAdManagerByType(:com.google.android.gms:90)
                                                   at com.google.android.gms.ads.internal.client.al.onTransact(:com.google.android.gms:91)
                                                   at android.os.Binder.transact(Binder.java:387)
                                                   at com.google.android.gms.ads.internal.client.zzv$zza$zza.zza(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zze.zza(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzl$1.zzim(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzl$1.zzin(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzl.zza(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzl.zza(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzae.zzjn(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzae.zzjm(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzae.zza(Unknown Source)
                                                   at com.google.android.gms.ads.BaseAdView.loadAd(Unknown Source)
                                                   at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
                                                   at com.xyz.xyz.xyz.MainActivity.LoadAd(MainActivity.java:539)
                                                   at com.xyz.xyz.xyz.MainActivity.onCreate(MainActivity.java:323)
                                                   at android.app.Activity.performCreate(Activity.java:6237)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
                                                   at android.app.ActivityThread.-wrap15(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:148)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:72

广告视图的代码如下:

private void LoadAd(){
    Log.d(TAG, "LoadAd() called");
    mAdView = new AdView(this);
    //Finds an ad that best fits a user's device.
    LinearLayout adContainer = (LinearLayout)   findViewById(R.id.adView_container);
    mAdView.setAdSize(Utils.getBannerSize(this));
    mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));
    adContainer.addView(mAdView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}

1 个答案:

答案 0 :(得分:2)

如果您将StrictMode设置为按detectAll()detectDiskReads()检测DiskReads,则当应用程序读取磁盘时会发生StrictMode违规。

我认为DiskReads对于正常情况来说不是问题。因此,您可以禁用以下违规行为:

StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder()
    . ...
    .permitDiskReads()
    .permitDiskWrites() // If you also want to ignore DiskWrites, Set this line too.
    . ...
    .build();

StrictMode.setThreadPolicy(threadPolicy);