简体   繁体   中英

LibGDX Admob | ad is loading but why it is not getting visible on the screen?

I want to implement the banner ads in a game I'm developing. I used the code bellow. It is not showing me any errors but it is not showing the ad as well. Please check the code and help me out.

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();
        }
    }
}

And here is the log message. The ad is loading and after every 60 seconds it should refresh but it is not even getting visible.

Here a short code snippet from my project.

To create a banner (not visible from the start):

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);
}

Those two methods are called from core module whenever banner needs to be enabled\\disabled:

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);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM