AdMob广告仅出现一次

时间:2018-06-24 18:10:23

标签: android android-layout android-studio mobile admob

有人可以帮助我解决这个问题吗?当我在模拟器中运行我的应用程序时,广告每次都会出现,但是当我在手机上运行时,广告只会出现一次,而不会再次出现。我正在为游戏使用Gdx库

Android类启动器

public class AndroidLauncher extends AndroidApplication implements AdHandler {

    private static final String TAG = "AndroidLauncher";
    private final int SHOW_ADS = 1;
    private final int HIDE_ADS = 0;
    protected AdView adView;

    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SHOW_ADS:
                    adView.setVisibility(View.VISIBLE);
                    break;
                case HIDE_ADS:
                    adView.setVisibility(View.GONE);
                    break;
            }
        }
    };

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout layout = new RelativeLayout(this);

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        View gameView = initializeForView(new BolsoBird(this), config);
        layout.addView(gameView);

        adView = new AdView(this);

        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                int visibility  = adView.getVisibility();
                adView.setVisibility(AdView.GONE);
                adView.setVisibility(visibility);
                Log.i(TAG,"Ad loaded...");
            }
        });
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId("xxxxxxx");

        AdRequest.Builder builder = new AdRequest.Builder();
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );

        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

        layout.addView(adView, adParams);

        adView.loadAd(builder.build());
        setContentView(layout);
    }

    @Override
    public void showAds(boolean show) {
        handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
    }
}

我正在使用游戏的创建方法来初始化广告。

public void create () {
         .....
         .....
        handler.showAds(toggle); //toggle is true every time..

    }

谢谢大家。

1 个答案:

答案 0 :(得分:0)

尝试将 AdView 放置在布局中,并更改布局 Visibility 。 设置可见性的正确方法是使用 View.GONE ,但是您已在 AdListener()中指定了 AdView.GONE