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();
}
}