如何在libgdx中显示具有多个屏幕的插页式广告?

时间:2019-04-24 11:03:51

标签: android libgdx admob

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

我想在触摸按钮时在BabyOrange.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();
}

}

1 个答案:

答案 0 :(得分:0)

这可能是媒体类型问题。据我所知LibGDX不支持HTML5,HTML5用于大多数android广告平台上的插页式广告。标语广告是包含广告的本地android视图,因此似乎可以正常工作。

我试图将全屏视频广告添加到我的LibGDX应用中,但也失败了(不支持的媒体类型)。一旦LibGDX中的HTML5支持按此问题的建议运行,也许就可以了:Fullscreen Libgdx HTML5 not working on mobile

如果找到解决方法,请分享:D