How to show interstitial ad with multiple screens

2019-08-01 03:20发布

I successfully implemented banner ad in my libgdx game, now i am trying to implementing interstitial ad. I want to show ad on the button click but the ad is not showing on the screen, i am using log to know if the ad is loading and it is loading everytime but not visible on the screen. i am using multiple screen classes in the game. I have watched several tutorials on it but nothing seems to work.

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

This is the 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 );

}
}

here is the interface

public interface AdService {

boolean isInterstitialLoaded();
void showInterstitial();
}

I want to show ads here in the BabyOrange.java when the button touched.

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条回答
看我几分像从前
2楼-- · 2019-08-01 03:45

It might be a mediatype issue. As far as i know LibGDX doesn't support HTML5 which are used for interstitial ads on most android advertising platforms. The banner ad is a native android view containing the ad so this seems to work.

I tried to add fullscreen video ads to my LibGDX app and failed as well (unsupported media type something). Maybe it would work once the HTML5 support in LibGDX works like suggested in this question: Fullscreen Libgdx HTML5 not working on mobile

If you find a way to solve it please share :D

查看更多
登录 后发表回答