Android admob 插页式广告在应用启动后仅显示一次
Android admob interstitial adds showing only once after the app starts
点击按钮,我会显示 admob 插页式广告。
第一次只显示添加,再次点击按钮不显示添加。
目前我指的是使用这个 google admob 示例 link 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();
}
}
});
插页式广告只会在应用启动后显示一次,因为您需要重新加载广告才能再次显示。
一旦启动,应用广告就会加载,您需要对其执行回调。展示广告后,您需要在展示之前再次加载广告。
您可以阅读官方文档以获得更好的理解。
Interstitial ads documentation
更新:
@Rishi 您需要在广告关闭后重新加载广告
@Override
public void onAdDismissedFullScreenContent() {
loadInterstitialAd(activity);
}
这是加载广告的代码
public void loadInterstitialAd(Context context){
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(context,your ad id, adRequest,interstitialCallback);
}
InterstitialAdLoadCallback interstitialCallback = new InterstitialAdLoadCallback(){
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
mInterstitialAd = null;
}
};
@NadeemAslam 是正确的。作为一个明确的替代方案,这也是我总是使用我在
中调用的方法来实现它的方式
protected void loadInterstitialAd() { //your method can be private or protected
if (interstitialAd == null) {
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd newIntad) {
interstitialAd = newIntad; //interstitialAd is static variable
interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
interstitialAd = null;
loadInterstitialAd(); //METHOD CALL WITHIN ITSELF
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
interstitialAd = null;
}
});
}
}
点击按钮,我会显示 admob 插页式广告。
第一次只显示添加,再次点击按钮不显示添加。
目前我指的是使用这个 google admob 示例 link 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();
}
}
});
插页式广告只会在应用启动后显示一次,因为您需要重新加载广告才能再次显示。 一旦启动,应用广告就会加载,您需要对其执行回调。展示广告后,您需要在展示之前再次加载广告。 您可以阅读官方文档以获得更好的理解。 Interstitial ads documentation
更新: @Rishi 您需要在广告关闭后重新加载广告
@Override
public void onAdDismissedFullScreenContent() {
loadInterstitialAd(activity);
}
这是加载广告的代码
public void loadInterstitialAd(Context context){
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(context,your ad id, adRequest,interstitialCallback);
}
InterstitialAdLoadCallback interstitialCallback = new InterstitialAdLoadCallback(){
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
mInterstitialAd = null;
}
};
@NadeemAslam 是正确的。作为一个明确的替代方案,这也是我总是使用我在
中调用的方法来实现它的方式 protected void loadInterstitialAd() { //your method can be private or protected
if (interstitialAd == null) {
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd newIntad) {
interstitialAd = newIntad; //interstitialAd is static variable
interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
interstitialAd = null;
loadInterstitialAd(); //METHOD CALL WITHIN ITSELF
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
interstitialAd = null;
}
});
}
}