本文介绍了Adview loadAd()触发StrictMode违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在应用程序中添加一些添加项。启用StrictMode后,我会收到一堆类似这样的策略违规事件(可能是6或7)。这正常吗?
I am trying to add some adds in my application. When I have StrictMode enabled, I get a bunch of policy violations like this one (maybe 6 or 7 of these). Is this normal?
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
adview的代码如下:
The code for the adview is like this:
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);
}
推荐答案
如果将StrictMode设置为通过 detectAll()
或 detectDiskReads()
进行DiskReads,在应用程序读取磁盘时会发生StrictMode冲突。
If you set StrictMode to detect DiskReads by detectAll()
or detectDiskReads()
, StrictMode violation occurs when app reads the disk.
对于正常的情况,我认为DiskReads并不是问题。因此,您可以像下面这样禁用这种违规行为:
I think DiskReads is not problem for normal situlations. So you can disable this violations like following:
StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder()
. ...
.permitDiskReads()
.permitDiskWrites() // If you also want to ignore DiskWrites, Set this line too.
. ...
.build();
StrictMode.setThreadPolicy(threadPolicy);
这篇关于Adview loadAd()触发StrictMode违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!