【发布时间】:2019-04-07 11:38:24
【问题描述】:
我使用 Interstitial AdMob 创建了一个应用程序,但是当它打开广告并关闭它时,它又重新打开,依此类推...我想做的是在 5 次启动计数应用程序后抛出广告。这是我的代码。
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());
}
});
其中 INTERSTITIAL_LAUNCH 是 4 的整数。
【问题讨论】:
标签: android admob interstitial