-1

public class Starting extends ActionBarActivity {

    private final String PREFERENCE_NAME = "ad_counter_preference"; //class level variable
    private final String COUNTER_INTERSTITIAL_ADS = "ad_counter"; //class level variable
    private int mAdCounter = 0; //class level variable

    //adview
    private AdView mAdView;

    //interstitial
    private String TAG = Starting.class.getSimpleName();
    InterstitialAd mInterstitialAd;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starting);

        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        mAdView.loadAd(adRequest);

        //interstitial
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd = new InterstitialAd(this);

        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));


        // Load ads into Interstitial Ads
        mInterstitialAd.loadAd(adRequest);

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
            }
        });

        SharedPreferences preferences = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putInt(COUNTER_INTERSTITIAL_ADS, mAdCounter);
        editor.commit();

        mAdCounter = preferences.getInt(COUNTER_INTERSTITIAL_ADS, 0);

        if (mAdCounter == 3) {
            // Load interstitial ad now
         showInterstitial();
            mAdCounter = 0; //Clear counter variable
        } else {
            mAdCounter++; // Increment counter variable
        }

        // Save counter value back to SharedPreferences
        editor.putInt(COUNTER_INTERSTITIAL_ADS, mAdCounter);
        editor.commit();

    }




    private void showInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }


//banner ads
    @Override
    public void onPause() {
        if (mAdView != null) {
            mAdView.pause();
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdView != null) {
            mAdView.resume();
        }
    }

    @Override
    public void onDestroy() {
        if (mAdView != null) {
            mAdView.destroy();
        }
        super.onDestroy();
    }
}

I am just about ready to launch my app but want it to be right and not annoy the users with interstials popping up all over the place so would like to control when they are loaded! on an activity i would like the interstitial to load on the 3rd time the activity has started! i have so far got the ad to load when the activity starts but at the moment it is starting everytime.

public class Starting extends ActionBarActivity {

    //adview
    private AdView mAdView;

    //interstitial
    private String TAG = Starting.class.getSimpleName();
    InterstitialAd mInterstitialAd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starting);

        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        mAdView.loadAd(adRequest);

        //interstitial
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd = new InterstitialAd(this);

        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));

       
        // Load ads into Interstitial Ads
        mInterstitialAd.loadAd(adRequest);

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
            }
        });
    }
    private void showInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }


//banner ads
    @Override
    public void onPause() {
        if (mAdView != null) {
            mAdView.pause();
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdView != null) {
            mAdView.resume();
        }
    }

    @Override
    public void onDestroy() {
        if (mAdView != null) {
            mAdView.destroy();
        }
        super.onDestroy();
    }
}
David Marsh
  • 147
  • 1
  • 3
  • 16

2 Answers2

1

You can use SharedPreferences in android. Whenever a user opens the specific activity, increment the field stored in sharedpreferences which serves as a counter. Meanwhile, check the counter all the time. And when it reaches three, load the interstitial ad.

You can refer the following code :

public class Starting extends ActionBarActivity {

private final String PREFERENCE_NAME = "ad_counter_preference";
private final String COUNTER_INTERSTITIAL_ADS = "ad_counter";
private int mAdCounter = 0;

private InterstitialAd mInterstitialAd;
private AdRequest mInterstitialAdRequest;
private AdRequest mBannerAdRequest;
private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    loadInterstitialAd();
    loadBannerAd();

    SharedPreferences preferences = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();

    mAdCounter = preferences.getInt(COUNTER_INTERSTITIAL_ADS, 0);

    if (mAdCounter == 3) {
        // Load interstitial ad now
        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
            }
        });
        mAdCounter = 0; //Clear counter variable
    } else {
        mAdCounter++; // Increment counter variable
    }

    // Save counter value back to SharedPreferences
    editor.putInt(COUNTER_INTERSTITIAL_ADS, mAdCounter);
    editor.commit();
}

private void loadInterstitialAd() {
    mInterstitialAdRequest = new AdRequest.Builder()
            .build();

    //interstitial
    mInterstitialAd = new InterstitialAd(this);

    // set the ad unit ID
    mInterstitialAd.setAdUnitId("Your Ad unit Id");

    // Load ads into Interstitial Ads
    mInterstitialAd.loadAd(mInterstitialAdRequest);
}

private void loadBannerAd() {
    mAdView = (AdView) findViewById(R.id.adView);
    mBannerAdRequest = new AdRequest.Builder()
            .build();
    mAdView.loadAd(mBannerAdRequest);
}

private void showInterstitial() {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    }
}

@Override
public void onPause() {
    if (mAdView != null) {
        mAdView.pause();
    }
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
    if (mAdView != null) {
        mAdView.resume();
    }
}

@Override
public void onDestroy() {
    if (mAdView != null) {
        mAdView.destroy();
    }
    super.onDestroy();
}}
Newbie
  • 368
  • 3
  • 12
  • tried this but the ad is still loading when the activity starts, i have placed the showInterstital() method after the counter == 3 in the if statement but still loads right away! prehaps im missing something or should i remove part of my code? if you could update my java i posted that might be easier! – David Marsh Jul 12 '16 at 13:03
  • i have added new java code wih the shared preferences – David Marsh Jul 13 '16 at 08:34
  • You are already calling the showInterstitial method in onCreate inside the setAdListener. That is the reason why Ad shows up all the time. Please remove that code. And place it inside the if condition check for mAdCounter == 3. – Newbie Jul 13 '16 at 09:15
  • should i put all the ad code in the if condition or should i wrap everything into a method and call the method in the if condition? quite confusing as never dealt with ads before – David Marsh Jul 13 '16 at 09:18
  • Remove the setAdlistener method from onCreate. Place the complete setAdListener code snippet to a method. And then call it inside the if condition check. – Newbie Jul 13 '16 at 09:25
  • It wont work for me, i have done as you say and moved the setAdlistener to the if condition but when i do this it gives an error! it seems the setAdlistener has to remain where it is in onCreate – David Marsh Jul 13 '16 at 09:41
  • just testing it now, the methods towards the bottom for my banner ads are now highlighted in red as the adview is no longer present! – David Marsh Jul 13 '16 at 10:20
  • no, the code as changed my other banner ad that sits at bottom of page! on my original post there is a private AdView mAdView; and that is linked to my banner ads, there are also 3 overidden methods towards bottom of my code! since updating the code these do no not work and left with errors becaus the updated code you gave me as changed all this – David Marsh Jul 13 '16 at 11:08
  • 1
    I have updated the answer with banner ads too. Check the code. – Newbie Jul 13 '16 at 11:35
  • the banner is showing but the interstitial is not showing, i have tried going back and entering the activity a number of times but interstitial does not seem to be showing – David Marsh Jul 13 '16 at 11:44
  • one moment, will try again as think its an error on my part – David Marsh Jul 13 '16 at 11:47
  • Perfect, we got there in the end! i realised i didnt put my ad unit ID in last time! but yes it definitely works, thanks alot for your time on this its really been a puzzle for me so i appreciate it – David Marsh Jul 13 '16 at 11:53
1

I would suggest using SharedPreferences. Put the following code everytime activity starts

SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);  
SharedPreferences.Editor editor = preferences.edit();

int displayTimes = preferences.getInt("kDisplayTimes", 0); 

if (displayTimes == 3) {
    // Shown 3 times, reset counter
    displayTimes = 0;

    // Show interstitial
}
else {
    // Less than 3 times, increase counter
    displayTimes++;
}

// Save counter back to SharedPreferences
editor.putInt("kDisplayTimes", displayTimes);
editor.commit();
Dickson Leonard
  • 518
  • 5
  • 14
  • i see, so i call the showInterstital() method to show the ad after the displaysTimes = 0; do i need to put all this in the on create? – David Marsh Jul 12 '16 at 12:47
  • I think onCreate would be the correct place to do it. I just edited my answer to check for condition `displayTimes == 3` instead of 0 because I noticed that on 0 the index will always get skipped – Dickson Leonard Jul 12 '16 at 13:39