AdMob横幅的可见性固定在View.GONE

时间:2019-03-14 03:00:37

标签: android libgdx admob

我已经有几个星期了。我最终将范围缩小到此:无论我多么努力地将AdView设置为View.GONE,AdView的可见性似乎都停留在View.VISIBLE上。

这是我的代码:

public class AndroidLauncher extends AndroidApplication {

    AdView bannerView;

    RelativeLayout layout;

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

        AppLovinSdk.initializeSdk(this);
        AppLovinPrivacySettings.setHasUserConsent(false, this);

        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); // test APP ID

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        game = new GameClass(this);

//      // Create Layout that will call both Libgdx View and AdMod View
        layout = new RelativeLayout(this);

        // InitializeCommonRessources Libgdx View
        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(game, config);
        layout.addView(gameView);

        // Tell Android to use this Layout
        setContentView(layout);

        setupBanner();
    }

    @Override
    public void setupBanner() {
        SetupAdMobBanner();
    }

    public void SetupAdMobBanner() {
        System.out.println("Setup AdMob BANNER");

        bannerView = new AdView(this);

        bannerView.setVisibility(View.VISIBLE); // These are my desperate attempts to make the banner visible
        System.out.println("visibility == " + bannerView.getVisibility()); // Always prints "visibility == 8" which is the value for View.GONE (View.VISIBLE is 0)

        bannerView.setAdListener(new AdListener() {

            @Override public void onAdOpened() {
                System.out.println("BANNER AD OPENED");
                Ressources.incrementBannerClicks();
            }

            @Override public void onAdClosed() {
                System.out.println("BANNER AD CLOSED");
            }

            @Override public void onAdLeftApplication() {
                System.out.println("BANNER LEFT APP");
            }

            @Override public void onAdFailedToLoad(int var1) {
                System.out.println("BANNER FAILED TO LOAD : " + var1);
                destroyAdMobBanner();
                SetupAdMobBanner();
            }

            @Override public void onAdLoaded() {
                System.out.println("BANNER LOADED");
                bannerView.setVisibility(View.VISIBLE);
                System.out.println("visibility == " + bannerView.getVisibility());
            }

            @Override public void onAdImpression() {
                System.out.println("BANNER IMPRESSION");
            }
        });

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

        bannerView.setAdSize(AdSize.SMART_BANNER);

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

        bannerView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // Test banners

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

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

        layout.addView(bannerView, adParams);

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

        loadAdMobBanner();

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());
    }

    public void loadAdMobBanner() {
        System.out.println("BANNER loadAd(adRequest)");
        AdRequest adRequest = new AdRequest.Builder().addTestDevice("36429449DBD95B918CFD96E610995AE6").build(); // Test ads for my Honor 7x
        bannerView.loadAd(adRequest);
    }

    public void destroyAdMobBanner() {
        if (bannerView == null) return;

        System.out.println("Destroying BANNER");

        layout.removeView(bannerView);
        bannerView.destroy();
        bannerView = null;
    }
}

如您所见,我极力尝试将可见性设置为VISIBLE,但是它总是在紧随其后打印visibility == 8(8为GONE的值,VISIBLE为0)。

最奇怪的是,有时横幅实际上会显示。 它可能会工作一两天,而突然却无法工作一两天,即使AdListener表示已成功加载,横幅也不会显示,我没有做任何事情。< / p>

问题出现在带有实时广告和测试广告的物理设备Honor 7X上。

2 个答案:

答案 0 :(得分:0)

您必须将测试设备添加到adView。您将在logcat中找到测试设备的ID。如果您在开发过程中未添加测试设备并多次点击广告,则该广告ID将会停止工作。

答案 1 :(得分:0)

仅添加此代码未设置可见性

尝试此代码

private AdView mAdView;

mAdView = (AdView) findViewById(R.id.adView);

AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                // Check the LogCat to get your test device ID
                .addTestDevice("36429449DBD95B918CFD96E610995AE6")
                .build();
        mAdView.loadAd(adRequest);


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

@Override
public void onDestroy() {
    if (mAdView != null) {
        mAdView.destroy();
    }
    super.onDestroy();
}

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

希望能对您有所帮助!

谢谢。