How to display a Interstitial Ad in a LibGDX project?

落爺英雄遲暮 提交于 2019-12-01 12:29:52

You already integrated banner Ad so no need to injected dependent artifact in your project.

Follow these steps for Interstitial Ad integration.

  1. AndroidManifest.xml

    Make an entry of AdActivity for Interstitial Ad

    <activity android:name="com.google.android.gms.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
              android:theme="@android:style/Theme.Translucent" />
    
  2. Create an interface inside your core module

    public interface AdService {
    
       boolean isInterstitialLoaded();
       void showInterstitial();
    }
    
  3. Create a parameterized constructor of FlappyGame class

    public AdService adService;
    
    public FlappyGame(AdService ads){
         adService=ads;
    }
    
  4. Implement AdService interface to your AndroidLauncher class

    public class AndroidLauncher extends AndroidApplication implements AdService {
    
        private static final String AD_UNIT_ID_INTERSTITIAL = "ca-app-pub-XXXXX/XXXXX";
        private InterstitialAd interstitialAd;
    
        @Override
        protected void onCreate (Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
    
             ...
             View gameView=initializeForView(new FlappyGame(this), config);
             ...
    
             interstitialAd = new InterstitialAd(this);
             interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
             interstitialAd.setAdListener(new AdListener() {
                  @Override
                  public void onAdLoaded() {}
    
                  @Override
                  public void onAdClosed() {
                       loadIntersitialAd();
                  }
             });
    
             loadIntersitialAd();
        }
    
        private void loadIntersitialAd(){
    
           AdRequest interstitialRequest = new AdRequest.Builder().build();
           interstitialAd.loadAd(interstitialRequest);
        }
    
      @Override
      public void showInterstitial() {
             runOnUiThread(new Runnable() {
                 public void run() {
                   if (interstitialAd.isLoaded())
                      interstitialAd.show();
                   else
                      loadIntersitialAd();
                 }
             });
       }
    
       @Override
       public boolean isInterstitialLoaded() {
            return interstitialAd.isLoaded();
       }
    }
    
  5. GameScreen class

    RunnableAction playWooshAction = Actions.run(new Runnable() {
            @Override
            public void run() {
                com.pentagames.flappybibi.Assets.playWooshSound();
                 game.adService.showInterstitial();
            }
    });
    

I integrated Interstitial Ad in your project, created a pull request for the same. You can merge my pull request.

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