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

2022-01-12 00:00:00 git android java libgdx admob

我发现了这个 Flappy Bird GIT 项目:https://github.com/barodapride/flappy-教程-巴罗达必利如何在此实施 BannerAd?没有布局文件.以及如何实施 InterstitialAd?如果我尝试写

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;

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

Then it says "cannot resolve symbol InterstitialAd"

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

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...

推荐答案

我在这里回答了 这里,如何将 AdMob 横幅广告集成到 LibGDX 项目中.

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

现在我在集成横幅广告后回答InterstitialAd集成.

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

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

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

编辑

你不能用类/接口名称调用非静态方法,showOrLoadInterstitial(),需要一个IActivityRequestHandler实现类的对象,所以创建一个参数化构造函数MyGdxGame 并从 android 模块传递引用.

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

MyGdxGame游戏类中捕获IActivityRequestHandler的引用

public class MyGdxGame extends Game {

   Public IActivityRequestHandler requestHandler;

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

   ...
}

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

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

相关文章