How can I implement AdMob Ads in a LibGDX Android project?

前端 未结 1 745
南笙
南笙 2020-12-12 04:44

I found this Flappy Bird GIT project: https://github.com/barodapride/flappy-tutorial-barodapride How can I implement the BannerAd in this? There is no layout file. And how c

相关标签:
1条回答
  • 2020-12-12 04:53

    I answered here, how you integrate AdMob banner Ad inside your LibGDX project.

    Now I answer InterstitialAd integration after Integration of banner Ad.

    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);
    
        ...
        ...
    
        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 showOrLoadInterstitial() {
    
         runOnUiThread(new Runnable() {
                public void run() {
                    if (interstitialAd.isLoaded()) 
                         interstitialAd.show();                         
                    else 
                         loadIntersitialAd();           
                }
          });
    }
    

    Need an interface to call showOrLoadInterstitial from core module so I created IActivityRequestHandler inside core module and implement this interface to AndroidLauncher of android module.

    public interface IActivityRequestHandler {
    
         void showOrLoadInterstitial();
    }
    

    EDIT

    You can't call non-static method, showOrLoadInterstitial() with class/interface name, need an object of IActivityRequestHandler implemented class so create a parameterized constructor of MyGdxGame and pass reference from android module.

     public class AndroidLauncher extends AndroidApplication implements IActivityRequestHandler  {
    
       @Override
       protected void onCreate (Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
    
           View gameView=initializeForView(new MyGdxGame(this));
           ....
           ....
       }
    

    Capture reference of IActivityRequestHandler inside MyGdxGame Game class

    public class MyGdxGame extends Game {
    
       Public IActivityRequestHandler requestHandler;
    
       public MyGdxGame(IActivityRequestHandler requestHandler){
           this.requestHandler=requestHandler;
       }
    
       ...
    }
    

    Now you've object reference, can called requestHandler.showOrLoadInterstitial()

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