在 activity 的第 3 次开始时加载插页式 admob 广告
Load interstitial admob advert on 3rd start of an activity
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();
}
}
我正准备启动我的应用程序,但希望它是正确的,并且不会因为到处弹出插页而惹恼用户,所以我想控制它们的加载时间!在 activity 上,我希望在 activity 第 3 次加载插页式广告!到目前为止,我已经让广告在 activity 开始时加载,但目前它每次都在开始。
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();
}
}
您可以在 android 中使用 SharedPreferences。
每当用户打开特定的 activity 时,增加存储在用作计数器的 sharedpreferences 中的字段。
同时,随时检查柜台。当它达到三个时,加载插页式广告。
您可以参考以下代码:
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();
}}
我建议使用 SharedPreferences。每次activity启动时放下面代码
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();
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();
}
}
我正准备启动我的应用程序,但希望它是正确的,并且不会因为到处弹出插页而惹恼用户,所以我想控制它们的加载时间!在 activity 上,我希望在 activity 第 3 次加载插页式广告!到目前为止,我已经让广告在 activity 开始时加载,但目前它每次都在开始。
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();
}
}
您可以在 android 中使用 SharedPreferences。 每当用户打开特定的 activity 时,增加存储在用作计数器的 sharedpreferences 中的字段。 同时,随时检查柜台。当它达到三个时,加载插页式广告。
您可以参考以下代码:
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();
}}
我建议使用 SharedPreferences。每次activity启动时放下面代码
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();