Admob interstital ads not displaying in android

守給你的承諾、 提交于 2019-12-12 05:44:31

问题


Hi I am trying to display interstital ads using admob but it's not displaying the ads. 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


回答1:


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);

    }

});



回答2:


You can only display the ad after it is loaded. When you are calling displayInterstitial(), your ad may not be loaded. Register for a listener and that will tell you when the loading is done.

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



回答3:


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();
  }
 }
} 


来源:https://stackoverflow.com/questions/25474475/admob-interstital-ads-not-displaying-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!