0

In my onCreate, I have the code:

    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId("ca-app-pub-1852329945819279/4025192744");

    // Create ad request.
    AdRequest adRequest1 = new AdRequest.Builder().build();

    // Begin loading your interstitial.
    interstitial.loadAd(adRequest1);  

Where "interstitial" is a private InterstitialAd interstitial.

I then display the interstitial using the following method:

public void displayInterstitial() {

    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if (interstitial.isLoaded()) {
                interstitial.show();
            }
            resetInterstitial();
        }
    });

}  

How can I display the interstitial (call the displayInterstitial() method) as soon as the interstitial is loaded? All help appreciated.

djinne95
  • 275
  • 1
  • 3
  • 10

1 Answers1

2

Register the event that notifies you when the interstitial has been loaded:

interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-1852329945819279/4025192744");

interstitial.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
       interstitial.show();
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
         // no interstitial available
         interstitial = null;
    }

});

// Create ad request.
AdRequest adRequest1 = new AdRequest.Builder().build();

// Begin loading your interstitial.
interstitial.loadAd(adRequest1);  

If I'm not mistaken, you don't need to check whether the interstitial has been loaded (since the event is only raised if and when it has been loaded) and it's called on the UI thread (so no need for runOnUiThread).

Codo
  • 75,595
  • 17
  • 168
  • 206