我正在libgdx游戏中实施admob标语广告。我已经制作了几个屏幕,并希望在每个屏幕上都显示横幅广告。我在启动器类中实现了横幅广告,并试图通过调用接口方法在屏幕类中进行显示,但是即使logcat中的测试设备ID未显示,广告也不会显示。
public class BabyOrange extends BabyScreen {
private BabyActor bg;
private BabyActor ph;
private AdsController adsController;
public BabyOrange(Game g,AdsController adsController){
super(g);
this.adsController = adsController;
}
public void create () {
adsController.showBannerAd();
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());
}
这是界面
public interface AdsController {
public void showBannerAd();
public void hideBannerAd();
}
还有Android启动器类
public class AndroidLauncher extends AndroidApplication implements
AdsController{
private static final String BANNER_ID = "ca-app-pub-
959117648XXXXXX/XXXXXXXX";
AdView bannerAd;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new
AndroidApplicationConfiguration();
//create a gameView and a bannerAd AdView
View gameView = initializeForView(new BabyGame(), config);
setupAds();
//define the layout
RelativeLayout layout = new RelativeLayout(this);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(bannerAd,params);
setContentView(layout);
}
public void setupAds(){
bannerAd = new AdView(this);
bannerAd.setVisibility(View.INVISIBLE);
bannerAd.setAdUnitId(BANNER_ID);
bannerAd.setAdSize(AdSize.SMART_BANNER);
}
@Override
public void showBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bannerAd.setVisibility(View.VISIBLE);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
bannerAd.loadAd(ad);
}
});
}
@Override
public void hideBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bannerAd.setVisibility(View.INVISIBLE);
}
});
}
}