【发布时间】:2021-11-24 17:43:13
【问题描述】:
单击按钮时,我会显示 admob 插页式添加。
第一次它只显示添加,再次点击按钮它不显示添加。
目前我指的是使用这个google admob示例链接https://developers.google.com/admob/android/interstitial
使用 SDK 版本 20
下面是代码:
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Toast.makeText(getApplicationContext(), " The ad was dismissed.\n",
Toast.LENGTH_LONG).show();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
Toast.makeText(getApplicationContext(), " The ad failed to show.\n",
Toast.LENGTH_LONG).show();
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Toast.makeText(getApplicationContext(), " The ad was shown\n",
Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
// Handle the error
Toast.makeText(getApplicationContext(),loadAdError +" custom interstitial ad wasn't ready yet\n",
Toast.LENGTH_LONG).show();
Log.i(TAG, loadAdError.getMessage());
mInterstitialAd = null;
}
});
showAd = findViewById(R.id.showAd);
showAd.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
} else {
Toast.makeText(getApplicationContext(), " The interstitial ad wasn't ready yet\n", Toast.LENGTH_LONG).show();
}
}
});
【问题讨论】:
-
每次展示广告后都需要加载一个新广告
标签: android sdk admob interstitial