Android : Cancel/Pause/Stop any Interstitial Ads

on Monday, September 15, 2014


I have an Android App which displays Interstitial Ads using Googles admob network. The good thing about admob is, its activity independent. I can load the ad in my MainActivity and whenever admob is ready with the ad, it will display it, even when I am not in MainActivity anymore. I already use the isLoaded() method to show the ad, and thats 90% of the time already working as desired.


But the other 10% it is not. There are certain activities which should remain adfree because the ad will decrease the quality of the game. If a phone is slow or the connection is slow and it takes a while to load an ad, the ad might pop up in the wrong activity.


Is there something I could call, which would close loaded ads and stops loading ads which are not displayed yet? This would take effect only on two activities whilst the others will remain as they are.


so in my main activity I call:



Ads.loadAdMobInterstitial(MainActivity.this);


and this is my class which handles Ads:



public class Ads {

public static InterstitialAd loadAdMobInterstitial(final Context context) {

int admobUnitId = string.admob_ad_unit_id;

final InterstitialAd iAd = new InterstitialAd(context);
iAd.setAdUnitId(context.getString(admobUnitId));

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

});

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();

iAd.loadAd(adRequest);

return iAd;
}
}

0 comments:

Post a Comment