Karnak
Karnak

Reputation: 113

AdMob Intersitial Ad remove delay

I check other topic there on SOF, and looking for some new knowledge. How to remove delay for showing Admob intersitial ad on this code

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0) {
    // Create the interstitial
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id));

    // Create ad request.
    adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
}

//initialise banner ad
this.BANNER_AD_UNIT_ID = getResources().getString(R.string.BannerAd_unit_id);
showBanner();
}

public void openAd() {
if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0)          {
    runOnUiThread(new Runnable() {
        public void run() {
            if (!interstitial.isLoaded()) {
                interstitial.loadAd(adRequest);
            }
            interstitial.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    interstitial.show();
                }

            });

        }
    });
}
}

originaly posted here: How to make Admob interstitial show without delay?

Im newbie and just learning programming, thats why I`m asking for example on this code :)

Edit: this is how openAd is called

public synchronized void GameOver() {
    if (lives_left > 0) {
        //success! - game passed - save score
        ScoreManager.save_localscore_simple(score_times[currentLevel], "" + currentLevel, false);

        if (Level_Buttons.length >= currentLevel + 1)
            ScoreManager.save_localscore_simple(unlocked, "unlock" + (currentLevel + 1));

        if (sound_success != 0 && !sound_muted)
            sp.play(sound_success, 1, 1, 0, 0, 1);

        //intersitial ad 
        ad_counter++;
        if (ad_counter >= getResources().getInteger(R.integer.ad_shows_every_X_gameovers)) {
            openAd();
            ad_counter = 0;
        } 


    } else {
        //game not passed
        if (sound_gameover != 0 && !sound_muted)
            sp.play(sound_gameover, 1, 1, 0, 0, 1);
    }


    //open interstitial ad
    ad_counter++;
    if (ad_counter >= getResources().getInteger(R.integer.ad_shows_every_X_gameovers)) {
        openAd();
        ad_counter = 0;
    }

Upvotes: 0

Views: 691

Answers (1)

William
William

Reputation: 20196

Your code will do what you ask. But it is not what you should do as it will annoy your users and probably get your Admob account banned.

What you should do is call interstitial.loadAd() early, probably in your onCreate().

public void onCreate() {
    // Create the interstitial
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id));

    interstitial.setAdListener(new AdListener() {
        public void onAdClosed() {
            // Create another ad request.
            final AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();
            interstitial.loadAd(adRequest);
        }
    });
    // Create ad request.
    final AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    interstitial.loadAd(adRequest);
}

Then at a natural break point in your app (in GameOver), call if (interstitial.isLoaded()) { interstitial.show(); }

Once you have returned from showing the ad (in AdListener.onAdClosed() you can call interstitial.loadAd() and repeat.

Upvotes: 1

Related Questions