【发布时间】:2021-07-05 14:16:05
【问题描述】:
我对 google_mobile_ads 有疑问。 https://pub.dev/packages/google_mobile_ads
他们在 Android 模拟器、iOS 模拟器和 iOS 发布应用中工作,但不在发布应用中。
我将展示我的代码......即使我读到如果广告出现在模拟器中,它们也应该在发布应用程序中工作。
home.dart
static List<String> adKeywords = [
"keyword1",
"keyword2",
];
@override
void initState() {
super.initState();
MobileAds.instance.initialize().then((InitializationStatus status) {
print('Initialization done: ${status.adapterStatuses}');
MobileAds.instance
.updateRequestConfiguration(RequestConfiguration(
tagForChildDirectedTreatment:
TagForChildDirectedTreatment.unspecified))
.then((void value) {
createInterstitialAd();
});
});
}
void createInterstitialAd() {
_interstitialAd ??= InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
// adUnitId: AdHelper.bannerAdUnitId, //this uses ad_helper.dart. code below
request: AdRequest(
// testDevices: <String>["kGADSimulatorID"],
keywords: adKeywords,
// contentUrl: 'http://example.com/bar.html',
nonPersonalizedAds: true,
),
listener: AdListener(
onAdLoaded: (Ad ad) {
print('${ad.runtimeType} loaded.');
_interstitialReady = true;
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('${ad.runtimeType} failed to load: $error.');
ad.dispose();
_interstitialAd = null;
createInterstitialAd();
},
onAdOpened: (Ad ad) => print('${ad.runtimeType} onAdOpened.'),
onAdClosed: (Ad ad) {
Helpers().showToast("Refer 5 users to turn ads off.");
print('${ad.runtimeType} closed.');
ad.dispose();
createInterstitialAd();
},
onApplicationExit: (Ad ad) =>
print('${ad.runtimeType} onApplicationExit.'),
),
)..load();
}
ad_helper.dart
import 'dart:io';
class AdHelper {
static String get bannerAdUnitId {
if (Platform.isAndroid) {
return "ca-app-pub-...";
} else if (Platform.isIOS) {
return "ca-app-pub-...";
} else {
throw new UnsupportedError("Unsupported platform");
}
}
}
AndroidManifest.xml - 从 Android 应用设置中添加 AppID
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-..."/>
我也在终端中看到了这些错误:
E/chromium( 5694): [ERROR:cookie_manager.cc(137)] Strict Secure Cookie policy does not allow setting a secure cookie for http://googleads.g.doubleclick.net/ for apps targeting >= R. Please either use the 'https:' scheme for this URL or omit the 'Secure' directive in the cookie value.
W/WebView ( 5694): java.lang.Throwable: A WebView method was called on thread 'FinalizerDaemon'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 2) {36e7d88} called on null, FYI main Looper is Looper (main, tid 2) {36e7d88})
W/WebView ( 5694): at android.webkit.WebView.checkThread(WebView.java:2592)
W/WebView ( 5694): at android.webkit.WebView.loadUrl(WebView.java:732)
W/WebView ( 5694): at com.google.android.gms.internal.ads.zzber.zzfl(com.google.android.gms:play-services-ads@@19.7.0:133)
W/WebView ( 5694): at com.google.android.gms.internal.ads.zzber.destroy(com.google.android.gms:play-services-ads@@19.7.0:495)
W/WebView ( 5694): at com.google.android.gms.internal.ads.zzbeq.destroy(com.google.android.gms:play-services-ads@@19.7.0:109)
W/WebView ( 5694): at com.google.android.gms.internal.ads.zzbyy.finalize(com.google.android.gms:play-services-ads@@19.7.0:43)
W/WebView ( 5694): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:291)
W/WebView ( 5694): at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:278)
W/WebView ( 5694): at java.lang.Daemons$Daemon.run(Daemons.java:139)
W/WebView ( 5694): at java.lang.Thread.run(Thread.java:923)
D/StrictMode( 5694): StrictMode policy violation: android.os.strictmode.WebViewMethodCalledOnWrongThreadViolation
D/StrictMode( 5694): at android.webkit.WebView.checkThread(WebView.java:2592)
D/StrictMode( 5694): at android.webkit.WebView.loadUrl(WebView.java:732)
D/StrictMode( 5694): at com.google.android.gms.internal.ads.zzber.zzfl(com.google.android.gms:play-services-ads@@19.7.0:133)
D/StrictMode( 5694): at com.google.android.gms.internal.ads.zzber.destroy(com.google.android.gms:play-services-ads@@19.7.0:495)
D/StrictMode( 5694): at com.google.android.gms.internal.ads.zzbeq.destroy(com.google.android.gms:play-services-ads@@19.7.0:109)
D/StrictMode( 5694): at com.google.android.gms.internal.ads.zzbyy.finalize(com.google.android.gms:play-services-ads@@19.7.0:43)
D/StrictMode( 5694): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:291)
D/StrictMode( 5694): at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:278)
D/StrictMode( 5694): at java.lang.Daemons$Daemon.run(Daemons.java:139)
D/StrictMode( 5694): at java.lang.Thread.run(Thread.java:923)
W/Ads ( 5694): Could not call loadUrl.
【问题讨论】: