【发布时间】:2016-07-12 10:00:15
【问题描述】:
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();
}
}
我正准备启动我的应用程序,但希望它是正确的,并且不会因为插页式广告到处弹出而惹恼用户,所以我想控制它们何时加载!在一项活动中,我希望在活动开始的第 3 次加载插页式广告!到目前为止,我已经让广告在活动开始时加载,但现在它每次都开始。
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();
}
}
【问题讨论】:
标签: java android android-studio admob interstitial