Admob和Awkward Libgdx显示行为

时间:2014-12-28 15:56:59

标签: java android mobile libgdx admob

我在SO中寻找了一些答案,但我找不到任何东西。我用android开发了一个带libgdx的游戏。我用admob添加了广告。游戏中的一切都很好,但游戏中没有显示广告。如果我按下我的手机的主页按钮,将游戏发送到后面并再次按下主页按钮并再次打开游戏。广告正确显示。如何修复此行为以从头开始展示广告?

MyCode如下

public class AndroidLauncher extends AndroidApplication {
AdView adView;
@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();



    RelativeLayout layout = new RelativeLayout(this);


    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);


    View gameView = initializeForView(new NewGame(), config);


    adView = new AdView(this); 
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("ca-app-pub-XXXXXXXXXXXX");
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    layout.addView(gameView);


    RelativeLayout.LayoutParams adParams =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

    layout.addView(adView, adParams);


    setContentView(layout);
}


@Override
protected void onPause() {
    adView.pause();
    super.onPause();
}

@Override
protected void onResume() {
    adView.resume();
    super.onResume();
}

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试在setContentView之前隐藏并显示广告。

adView.setVisibility(View.GONE);
adView.setVisibility(View.VISIBLE);

如果这不起作用,只需为您的广告观看广告并做同样的事情

 adView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        adView.setVisibility(View.GONE);
        adView.setVisibility(View.VISIBLE);
    }        
});