Preloading Interstitial ads

3.4k views Asked by At

I am running interstitial ads in my app, but I am not sure how to preload them so they appear to the user to be instantaneous. Often it can take up to 10 seconds, and the user has already moved on to different areas of the app, it's very annoying.

This is the relevant code from the app:

private void launchInterstitial() {
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxx");

    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            showAdInter();
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            String message = String.format("onAdFailedToLoad (*a)", getErrorReason(errorCode));
        }

        @Override
        public void onAdClosed() {
            if (exitApp) {
                finish();
            }
        }
    });
}

private void showAdInter() {
    if (interstitialAd.isLoaded()){
        interstitialAd.show();
    } else {
        Log.d("","Interstitial ad failed to load");
    }
}

public void loadInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("Fxxxxxxxxxxxxxxxxxxxxx")
            .build();

    interstitialAd.loadAd(adRequest);
}

I have tried calling launchInterstitial() and loadInterstitial() when the activity launches and then call showAdInter() when I want the ad to appear, but it doesn't work that way.

Anyone have any ideas?

2

There are 2 answers

0
Bisclavret On BEST ANSWER

Can't believe no one known anything about this, or is unwilling to help. I will post the answer here so hopefully this will actually help someone in need. I call the following method in onCreate():

requestNewInterstitial();

Method is as follows:

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
            .build();

    mInterstitialAd.loadAd(adRequest);
}

I create an adlistener to reload the ad again in the background after one has been closed:

mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
        }
    });

And I call requestNewInterstitial(); on my onBackPressed() method.

This produces smooth results, interstitials appear instantaneously.

0
Saggy On

I was searching for Preloading Interstitial Ads, but unfortunately i didn't get complete and working answer for this.Somehow with some experiments, i able to achieve this mechanism. Here is my answer :

 public void showInterestialAd(final Context context, final String screenName){
    if (!YouTubeShareClass.getInstance().isInterestitialAdVisible) {
        if(mInterstitialAd == null) {
            mInterstitialAd = new InterstitialAd(context);
            mInterstitialAd.setAdUnitId(context.getString(R.string.interstitial_full_screen));
        }
        showInterstitial(context,screenName);
    }
}

private void showInterstitial(final Context context, final String screenName) {
    if (mInterstitialAd.isLoaded()) {
        displayAd(context,screenName);
        Toast.makeText(context,"Pre loaded",Toast.LENGTH_LONG).show();
    } else {
        AdRequest request = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxxxxx")
                    .build();

        mInterstitialAd.loadAd(request);
        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                super.onAdLoaded();
                Toast.makeText(context,"New loaded",Toast.LENGTH_LONG).show();
                if(mInterstitialAd.isLoaded()) {
                    displayAd(context,screenName);
                }
            }
            public void onAdClosed() {
                super.onAdClosed();
                YouTubeShareClass.getInstance().isInterestitialAdVisible = false;
                YouTubeShareClass.getInstance().loadAdRequest();
            }
        });
    }
}

private void displayAd(Context context,String screen) {
    mInterstitialAd.show();
    YouTubeShareClass.getInstance().isInterestitialAdVisible = true;
}

public void loadAdRequest() {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxxxxxx")
            .build();
    mInterstitialAd.loadAd(adRequest);
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            //do nothing... leave empty here...
        }
        @Override
        public void onAdClosed() {
            super.onAdClosed();
            YouTubeShareClass.getInstance().isInterestitialAdVisible = false;
            YouTubeShareClass.getInstance().loadAdRequest();
        }
    });
}

NOTE: - YouTubeShareClass.getInstance().isInterestitialAdVisible, variable is used to take care that no two interestial add will be showing at a time, to improve user experience. - When we create preloaded interstitial ad, make onAdLoaded() method, don't anything there. - I implemented this methods in Singleton class, that's why it has such implementation. You can change according to you need.