Android 如何在libgdx中用多个屏幕显示间隙广告?

Android 如何在libgdx中用多个屏幕显示间隙广告?,android,libgdx,admob,Android,Libgdx,Admob,我在我的libgdx游戏中成功地实现了横幅广告,现在我正在尝试实现中间广告。我想在点击按钮时显示广告,但广告没有显示在屏幕上,我使用日志来了解广告是否正在加载,它是否每次都在加载,但在屏幕上不可见。我在游戏中使用多个屏幕类。我已经看了好几篇关于它的教程,但似乎都不管用 public class AndroidLauncher extends AndroidApplication implements AdService{ private InterstitialAd interstitialA

我在我的libgdx游戏中成功地实现了横幅广告,现在我正在尝试实现中间广告。我想在点击按钮时显示广告,但广告没有显示在屏幕上,我使用日志来了解广告是否正在加载,它是否每次都在加载,但在屏幕上不可见。我在游戏中使用多个屏幕类。我已经看了好几篇关于它的教程,但似乎都不管用

public class AndroidLauncher extends AndroidApplication implements AdService{

private InterstitialAd interstitialAd;
private ScheduledExecutorService scheduler;


private static final String BANNER_ID = "ca-app-pub-3940256099942544/6300978111";

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

    //create a gameView and a bannerAd AdView

    RelativeLayout layout = new RelativeLayout(this);

    // Do the stuff that initialize() would do for you
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    // Create the libgdx View
    View gameView = initializeForView(new BabyGame(this), config);

    // Create and setup the AdMob view
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(BANNER_ID); // Put in your secret key here

    AdRequest adRequest = new AdRequest.Builder().addTestDevice("6D0D171A0065AD4E19656B813BC8F493").build();
    adView.loadAd(adRequest);

    // Add the libgdx view
    layout.addView(gameView);

    // Add the AdMob view
    RelativeLayout.LayoutParams adParams =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adParams.addRule(RelativeLayout.CENTER_IN_PARENT);

    layout.addView(adView, adParams);

    // Hook it all up
    setContentView(layout);

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    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();
}
}
这是BabyGame.java

public class BabyGame extends Game
{
public AdService adService;
BabyGame(AdService ads){
    adService = ads;
}

public void create()
{
    BabyOrange bp = new BabyOrange(this);
    setScreen( bp );

}
}
这里是界面

public interface AdService {

boolean isInterstitialLoaded();
void showInterstitial();
}
我想在按下按钮时在babyrange.java中显示广告

public class BabyOrange extends BabyScreen {

private BabyActor bg;
private BabyActor phone;
private ImageButton buttonLeft,buttonRight;

public AdService adService;

private Table table;

private AssetManager asset;
private TextureAtlas atlas;

 public BabyOrange(Game g){
     super(g);
 }

@Override
public void create() {

   asset = new AssetManager();
   asset.load("background-orange.png",Texture.class);
   asset.load("orange-ph.png",Texture.class);
   asset.finishLoading();

    bg = new BabyActor();
    bg.setTexture(asset.get("background-orange.png",Texture.class));
    bg.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

    ph = new BabyActor();
    ph.setTexture(asset.get("orange-ph.png",Texture.class));
    ph.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

    TextureRegion btLeft = new TextureRegion(new Texture("NUMBEROFF.png"));
    Drawable drawableLeft = new TextureRegionDrawable(new TextureRegion(btLeft));
    buttonLeft = new ImageButton(drawableLeft);

    TextureRegion btRight = new TextureRegion(new Texture("VEHICLEOFF.png"));
    Drawable drawableRight = new TextureRegionDrawable(new TextureRegion(btRight));
    buttonRight = new ImageButton(drawableRight);

    stage.addActor(bg);
    stage.addActor(phone);

    Gdx.input.setInputProcessor(stage);

    buttonRight.addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {


            game.setScreen(new BabyGreen(game));
            RunnableAction playWooshAction = Actions.run(new Runnable() {
                @Override
                public void run() {
                    adService.showInterstitial();
                }
            });
            return true;
        }
    });

    buttonLeft.addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

            game.setScreen(new BabyBlue(game));
            return true;
        }
    });

    table = new Table();
    table.padLeft(40);
    table.setPosition(phone.getWidth()/2,phone.getHeight()/2*0.6f);
    table.row().size(stage1.getWidth()/100*20,stage1.getWidth()/100*20);
    table.add(buttonLeft);
    table.add(buttonCenter);
    table.add(buttonRight);
    stage.addActor(table);
}


public void dispose(){

   bg.getRegion().getTexture().dispose();
   phone.getRegion().getTexture().dispose(); 
   stage.dispose();
   stage1.dispose();
}

}

这可能是一个mediatype问题。据我所知,LibGDX不支持HTML5,在大多数android广告平台上,HTML5用于间隙广告。横幅广告是一个包含广告的本地android视图,因此这似乎是可行的。我试图将全屏视频广告添加到我的LibGDX应用程序中,但也失败了(不支持媒体类型)。如果LibGDX中的HTML5支持能够像这个问题中建议的那样工作,那么它可能会工作:如果你找到一种解决方法,请分享:你可以在这里找到这个解决方案: