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

▼魔方 西西 提交于 2019-11-27 08:44:14

问题


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 can I implement the InterstitialAd? If I try to write

public InterstitialAd mInterstitialAd;

Then it says "cannot resolve symbol InterstitialAd"

I know how to do this in a normal Android Studio project, but this is the first time I'm working with libGDX and this is so complicated...


回答1:


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



来源:https://stackoverflow.com/questions/44485172/how-can-i-implement-admob-ads-in-a-libgdx-android-project

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