简体   繁体   English

Admob Interstital广告未在Android中展示

[英]Admob interstital ads not displaying in android

Hi I am trying to display interstital ads using admob but it's not displaying the ads. 嗨,我正在尝试使用admob显示插页式广告,但未显示广告。 My code is as follows 我的代码如下

  import com.google.android.gms.ads.AdRequest;
  import com.google.android.gms.ads.InterstitialAd;
  private static final String AD_UNIT_ID="ca-app-pub-21740939xxxxx";
  private InterstitialAd interstitial;
  @SuppressLint("NewApi")
  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId(AD_UNIT_ID);
    AdRequest adRequest = new AdRequest.Builder().build();
    interstitial.loadAd(adRequest);
    displayInterstitial();
  }
   public void displayInterstitial() {
        if (interstitial.isLoaded()) {
          interstitial.show();
        }
      }
  } 

In manifest I declared permissions: 在清单中,我声明了权限:

        <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

But it's not displaying. 但是没有显示。 Please help me to solve the issue 请帮我解决问题

You can only display the ad after it is loaded. 广告只能在加载后显示。 When you are calling displayInterstitial() , your ad may not be loaded. 当您调用displayInterstitial() ,您的广告可能无法加载。 Register for a listener and that will tell you when the loading is done. 注册一个侦听器,它会告诉您何时完成加载。

interstitial.setAdListener(new AdListener(){
    public void onAdLoaded(){
        displayInterstitial();
    }
});

I guess while you are displaying your ad it is not loaded yet , so try this 我想您正在展示广告时尚未加载它,因此请尝试此操作

interstitial= new InterstitialAd(this);
interstitial.setAdUnitId(AD_UNIT_ID);
interstitial.loadAd(new AdRequest.Builder().build();
}

interstitial.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        super.onAdLoaded();
        interstitial.show();  // you can call this anywhere you want 

    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        super.onAdFailedToLoad(errorCode);

    }

});

I am using the following code to display interstitial ad. 我正在使用以下代码来展示插页式广告。 It works perfect for me. 它对我来说很完美。

import com.google.android.gms.ads.AdRequest;

import com.google.android.gms.ads.InterstitialAd;

private InterstitialAd interstitial;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(“**************************”);

AdRequest adRequest = new AdRequest.Builder().build();

interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener(){
          public void onAdLoaded(){
               displayInterstitial();
          }
});
}

public void displayInterstitial() {
if (interstitial.isLoaded()) {
  interstitial.show();
  }
 }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM