How to display a Interstitial Ad in a LibGDX project?

后端 未结 1 1107
借酒劲吻你
借酒劲吻你 2021-01-16 12:31

I found a Flappy Bird GitHub project and changed it a little bit. I successfully implemented the AdMob Banner.

But now I also want a Interstitial Ad which pops up w

相关标签:
1条回答
  • 2021-01-16 13:32

    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.

    0 讨论(0)
提交回复
热议问题