如何在LibGDX And​​roid项目中实施AdMob广告?

时间:2017-06-11 14:53:23

标签: java android git libgdx admob

我找到了这个Flappy Bird GIT项目:https://github.com/barodapride/flappy-tutorial-barodapride 如何在此实施BannerAd? 没有布局文件。 我如何实施InterstitialAd? 如果我试着写

public InterstitialAd mInterstitialAd;

然后它说"无法解析符号InterstitialAd"

我知道如何在普通的Android Studio项目中执行此操作,但这是我第一次使用libGDX而且这太复杂了......

1 个答案:

答案 0 :(得分:0)

我回答了here,了解了如何在您的LibGDX项目中集成AdMob横幅广告。

现在,我在整合横幅广告后回答InterstitialAd整合。

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

需要一个接口来从核心模块调用showOrLoadInterstitial,所以我在核心模块中创建了IActivityRequestHandler并将此接口实现到了android模块的AndroidLauncher

public interface IActivityRequestHandler {

     void showOrLoadInterstitial();
}

修改

您不能使用类/接口名称调用非静态方法showOrLoadInterstitial(),需要IActivityRequestHandler实现类的对象,因此创建MyGdxGame的参数化构造函数并传递引用android模块。

 public class AndroidLauncher extends AndroidApplication implements IActivityRequestHandler  {

   @Override
   protected void onCreate (Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       View gameView=initializeForView(new MyGdxGame(this));
       ....
       ....
   }

IActivityRequestHandler游戏类

中捕获MyGdxGame的引用
public class MyGdxGame extends Game {

   Public IActivityRequestHandler requestHandler;

   public MyGdxGame(IActivityRequestHandler requestHandler){
       this.requestHandler=requestHandler;
   }

   ...
}

现在您有对象引用,可以调用requestHandler.showOrLoadInterstitial()