LibGDX Admob |广告正在加载,但为什么它不会在屏幕上显示?

时间:2018-02-05 11:35:16

标签: java android libgdx admob

我想在我正在开发的游戏中实施横幅广告。我用下面的代码。它没有向我显示任何错误,但它也没有显示广告。 请检查代码并帮助我。

public class AndroidLauncher extends AndroidApplication {

    public static final String TAG = "Android Launcher";
    protected AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "MyWakelockTag");
        wakeLock.acquire();

        RelativeLayout relativeLayout = new RelativeLayout(this);
        View gameView = initializeForView(new core_class(), config);
        relativeLayout.addView(gameView);

        adView = new AdView(this);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                Log.d(TAG, "\n\nAd Loaded...........!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
            }
        });
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        AdRequest builder = new AdRequest.Builder().addTestDevice("7655E37545A982D6CC7006F8088B40AC").build();
        //      builder.addTestDevice("7655E37545A982D6CC7006F8088B40AC");

        RelativeLayout.LayoutParams adPrams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );

        relativeLayout.addView(adView, adPrams);
        adView.loadAd(builder);
        setContentView(relativeLayout);

        initialize(new core_class(), config);
    }

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

    @Override
    protected void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }
}

这是日志消息。广告正在加载,每60秒后它应该刷新,但它甚至不可见。

1 个答案:

答案 0 :(得分:0)

这是我项目的简短代码段。

创建横幅(从头开始不可见):

private void createAdView() {
    bannerAd = new AdView(this);
    bannerAd.setAdSize(AdSize.SMART_BANNER);
    bannerAd.setAdUnitId(AD_UNIT_ID_BANNER);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    bannerAd.setLayoutParams(params);
    bannerAd.setBackgroundColor(Color.TRANSPARENT);
    bannerAd.setVisibility(View.INVISIBLE);
}

只要需要启用横幅\禁用,就会从core模块调用这两种方法:

public void showBannerAds() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            bannerAd.setVisibility(View.VISIBLE);
            bannerAd.setEnabled(true);
            AdRequest adRequest = new AdRequest.Builder()
                    .build();
            bannerAd.loadAd(adRequest);
        }
    });
}

public void hideBannerAds() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            bannerAd.setVisibility(View.GONE);
            bannerAd.setEnabled(false);
        }
    });
}