-2

I have created an app with Interstitial AdMob, but, when it opens the ad and I close it, it reopens again and so on... What I want to do is to throw the ad after 5 launch count app. Here's my code.

    sharedPreferences = getPreferences(0);
    int count = sharedPreferences.getInt("numRun", 0);
    count++;
    sharedPreferences.edit().putInt("numRun", count).commit();

    MobileAds.initialize(this, "ADMOB_ID");
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getString(R.string.INTERSTITIAL_ID));

    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    mInterstitialAd.setAdListener(new AdListener(){
        @Override
        public void onAdLoaded() {
            int count = sharedPreferences.getInt("numRun", 0);

            if (count == INTERSTITIAL_LAUNCH) {
                count = 0;
                sharedPreferences.edit().putInt("numRun", count).commit();
                mInterstitialAd.show();
            }
        }
        @Override
        public void onAdClosed() {
            // Code to be executed when the interstitial ad is closed.
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });

Where INTERSTITIAL_LAUNCH is an int of 4.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Esteban Chornet
  • 928
  • 3
  • 14
  • 38

1 Answers1

0

You said that INTERSTITIAL_LAUNCH is an int of 4.

When you check variable count against this value then according to the code if its value is greater than 4 then also the ad will be shown.

Replace that with:

if (count == 5)
Gourav
  • 2,746
  • 5
  • 28
  • 45
  • You were right! Also, the problem was that I was not setting count = 0, so whenever it gets 5, it still 5...and so on. Thaks! – Esteban Chornet Apr 07 '19 at 12:59
  • You can also set to make the count 0 after every ad show to continue the loop. – Gourav Apr 07 '19 at 13:01
  • And what if I want to throw the ad when I have a page load count of 15 of a webview? How do I control de count of the page visited of the webview? @Gourav – Esteban Chornet Apr 08 '19 at 07:23
  • That's also very easy. Increase the value of variable in the swipe refresh listener. If you have another question please ask a new one. This one was just for ads – Gourav Apr 08 '19 at 09:02
  • It is the same, I'm asking about Interstitial Ad but instead of 5 launch count app, 15 pages count visited through webview. – Esteban Chornet Apr 08 '19 at 12:07
  • The process would be same. Increase the variable value on each call of swipe refresh listener and check for if the value is 15 and then show add reset value. – Gourav Apr 08 '19 at 12:20