Search code examples
androidinterstitialadmob

How to request new interstitial ad in android


I have an activity to show a full-screen image. There is a button to download the image. Before downloading, I need to show an interstitial ad. After download, I need to request & load a new interstitial ad. I have coded the same as following:

public class FullScreenViewActivity extends Activity implements OnClickListener {
         private InterstitialAd interstitial;
         private AdRequest adRequest;
         ....

          @Override
            protected void onCreate(Bundle savedInstanceState) {
             interstitial = new InterstitialAd(FullScreenViewActivity.this);
             interstitial.setAdUnitId(getString(R.string.admob_interstitial_id)); //live ad unit
            ...
             }

            protected void onResume() {
                super.onResume();
                adRequest = getInterstitialAdRequest();
                interstitial.loadAd(adRequest);
                interstitial.setAdListener(new AdListener() {
                    public void onAdClosed() {
                        downloadImage();
                        adRequest = getInterstitialAdRequest();
                        interstitial.loadAd(adRequest);
                    }

                    public void onAdFailedToLoad(int var1) {
                        downloadImage();
                        adRequest = getInterstitialAdRequest();
                        interstitial.loadAd(adRequest);
                    }

                    public void onAdLeftApplication() {
                        downloadImage();
                        adRequest = getInterstitialAdRequest();
                        interstitial.loadAd(adRequest);
                    }
                });
            }

        public AdRequest getInterstitialAdRequest() {
            return new AdRequest.Builder().build();
        }
    }

My problem is that I keep on seeing the same ad every time I click the download button. Is there some problem in my logic? Or is it that admob is giving me same ad everytime I request?


Solution

  • I don't see anything in your code that would cause you to get the same ad repeatedly. It's possible that wherever you are in the world, there is one particular advertiser buying up almost all of the demand, which can cause a single ad to appear repeatedly. Usually it's a temporary issue, and will go away with time.

    I would recommend taking the loadAd calls out of these methods, though:

    public void onAdFailedToLoad(int var1) {
        downloadImage();
        adRequest = getInterstitialAdRequest();
        interstitial.loadAd(adRequest);
    }
    
    public void onAdLeftApplication() {
        downloadImage();
        adRequest = getInterstitialAdRequest();
        interstitial.loadAd(adRequest);
    }
    

    onAdLeftApplication is called when the user taps on an ad and a browser (or another app) is opened in response. When they return, onAdClosed will still be called, so as your code stands, you'll end up with two calls to loadAd. I'd remove the one in onAdLeftApplication and leave the one in onAdClosed.

    Calling loadAd a second time to recover from a failed ad in onAdFailedToLoad is a good idea, but you don't appear to have any circuit-breaking logic or increase in the time before trying again in the case of repeated failures. This could cause an infinite loop if the user's network connection goes down (Ad #1 fails immediately, which causes Ad #2 to try and load, which fails immediately and causes Ad #3...). Try using a counter to cut off the attempts after a certain number, or use a Handler to introduce a gradually increasing delay.

    Also, one final note: If you're testing your app, you should use test ads!