我是Android开发的新手。我有一个Web View应用程序。我目前的代码Admob interstitial只加载一次。任何人都可以帮我在网页加载后如何设置Admob非页内加载
AdRequest adRequest = new AdRequest.Builder().build();
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
答案 0 :(得分:0)
这是代码
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
//web page loaded , show your interstitial
}
});
答案 1 :(得分:0)
我有同样的问题。这对我有用(在Flutter中):
import 'package:firebase_admob/firebase_admob.dart';
InterstitialAd _interstitialAd;
bool _isInterstitialAdReady;
void prepAd(){
initState();
_loadInterstitialAd();
}
void runAd() {
_interstitialAd.show();
}
void initState() {
_isInterstitialAdReady = false;
_interstitialAd = InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
listener: _onInterstitialAdEvent,
);
}
void _loadInterstitialAd() {
_interstitialAd.load();
}
// TODO: Implement _onInterstitialAdEvent()
void _onInterstitialAdEvent(MobileAdEvent event) {
switch (event) {
case MobileAdEvent.loaded:
_isInterstitialAdReady = true;
break;
case MobileAdEvent.failedToLoad:
_isInterstitialAdReady = false;
print('Failed to load an interstitial ad');
break;
case MobileAdEvent.closed:
dispose();
prepAd();
break;
default:
// do nothing
}
}
void dispose() {// TODO: Dispose InterstitialAd object
_interstitialAd?.dispose();
}
我一开始运行一次prepAd(),然后在需要时运行runAd()。您可以根据需要在runAd()中检查_isInterstitialAdReady == true