Admob直到第二次请求才会显示广告

时间:2014-04-28 16:53:38

标签: android libgdx admob

关于这个和一些不同的答案有几个问题 - 这些答案似乎都不适合我。

我有一个libdgx应用程序,这让我觉得有点复杂,但我不知道它应该导致问题。我的onCreate代码如下。

我已经分配了一个在加载add时调用的AdListener。这个代码被击中但没有显示任何内容。如果我将广告设置为刷新,那么它将在第一次刷新后显示。如果我在短暂等待后第二次调用loadAd,则会显示横幅。如果我在两行连续的代码上调用loadAd,广告将不会显示。关于这里可能会发生什么的任何指示都将不胜感激。

编辑:我也注意到我可以点击广告,即使我看不到广告。当我返回到点击广告的游戏时,广告正确显示。

edit2:我将广告更改为SMART_BANNER而不是BANNER,这似乎按预期工作。我仍然不知道为什么BANNER不工作。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create the layout
    FrameLayout layout = new FrameLayout(this);


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

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.useAccelerometer = false;
    config.useCompass = false;
    config.useWakelock = false;
    config.useImmersiveMode = true;
    config.hideStatusBar = true;

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

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

    adView = new AdView(this);
    adView.setAdUnitId("xxxxxxxxxxxx");
    adView.setAdSize(AdSize.BANNER);
    adView.setAdListener(new AdListener() {
                             public void onAdLoaded()
                                 Gdx.app.log("Advert", "Ad Loaded");
                             }
                         }
    );

    // Add the AdMob view
    FrameLayout.LayoutParams adsParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT, android.view.Gravity.TOP | android.view.Gravity.CENTER_HORIZONTAL);
    layout.addView(adView, adsParams);

    // Hook it all up
    setContentView(layout);

    adView.loadAd(new AdRequest.Builder().build());
}

3 个答案:

答案 0 :(得分:5)

解决这个问题最终就像添加行

一样简单
adView.setBackgroundColor(Color.TRANSPARENT);

我的最终广告制作代码如下所示:

private void createAdView() {
    abBuilder = new AdRequest.Builder();
    abBuilder.addTestDevice("xxxxxxx");

    if (adView != null)
        ((ViewManager) adView.getParent()).removeView(adView);

    adView = new AdView(this);
    adView.setAdUnitId("xxxxxxx");
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdListener(new AdListener() {
                             public void onAdLoaded() {
                                 if (adsShown)
                                     adView.setVisibility(View.VISIBLE);
                                 Gdx.app.log("Advert", "Ad Loaded");
                             }
                         }
    );

    adView.setBackgroundColor(Color.TRANSPARENT);

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

    layout.addView(adView, adParams);
}

答案 1 :(得分:0)

只是想一想,你有这个代码吗?所有广告都需要它。

public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

    @Override
    public void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }

    /** Called before the activity is destroyed. */
    @Override
    public void onDestroy() {
        // Destroy the AdView.
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

答案 2 :(得分:0)

我在智能横幅上遇到了同样的问题,我通过在广告加载后通过AdListener将视图添加到布局来修复它:

        // Create and setup the AdMob view
        AdRequest.Builder ad_builder = new AdRequest.Builder();
        ad_builder.addTestDevice("XXXXXXXXXXXXX"); // my HTC phone
        m_AdRequest = ad_builder.build();

        m_AdView = new AdView(this);
        m_AdView.setAdSize(AdSize.SMART_BANNER);
        m_AdView.setAdUnitId("ZZZZZZZZZZZZ");
        m_AdView.setAdListener(new AdListener() {
            private boolean _first = true;

            @Override
            public void onAdLoaded() {
                if (_first) {
                    _first = false;

                    // Add the AdMob view when the first ad gets loaded, this makes sure the first ad gets displayed
                    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); // places the ad at the bottom overlapping your game

                    layout.addView(m_AdView, adParams);
                }
            }
        });

希望这能解决你的头痛问题!