当前,我正在使用20秒后显示Admob间隙广告的代码,当广告关闭时,我想在60秒后显示另一个广告,这是当前的代码,我是我是使用第一个广告在20秒后加载
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
interAd = new InterstitialAd(MainActivity.this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
interAd.loadAd(adRequest);
interAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
interAd.show();
}
});
interAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
Log.i("Ads", "onAdClosed");
}
});
}
} , 20000);
我尝试了从ADMOB教程中使用此方法加载广告,但是每秒
interAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Load the next interstitial.
interAd.loadAd(new AdRequest.Builder().build());
}
});
您应该将广告的生命周期附加到活动的生命周期中,以便您可以在用户可见应用时开始/停止显示添加。
为您的广告和处理程序创建一个全局变量
private InterstitialAd mInterstitialAd;
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
// Wait 60 seconds
mHandler.postDelayed(this, 60*1000);
// Show Ad
showInterstitial();
}
};
然后...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the InterstitialAd and set the adUnitId
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
@Override
protected void onStart() {
super.onStart();
// Start showing Ad in every 60 seconds
//when activity is visible to the user
mHandler = new Handler();
//mHandler.post(mRunnable);
// Run first add after 20 seconds
mHandler.postDelayed(mRunnable,20*1000);
}
protected void onStop() {
super.onStop();
// Stop showing Ad when activity is not visible anymore
mHandler.removeCallbacks(mRunnable);
}
private void loadInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.setRequestAgent("android_studio:ad_template").build();
mInterstitialAd.loadAd(adRequest);
}
private InterstitialAd newInterstitialAd() {
InterstitialAd interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
mNextLevelButton.setEnabled(true);
}
@Override
public void onAdFailedToLoad(int errorCode) {
mNextLevelButton.setEnabled(true);
}
@Override
public void onAdClosed() {
// Reload ad so it can be ready to be show to the user next time
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
});
return interstitialAd;
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and reload the ad.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
}
制作一个新线程,然后在显示您的广告后,添加60000毫秒的睡眠时间。它将允许异步工作。
示例,在onAdClosed()
中添加此代码:
new Thread(new Runnable(){
public void run(){
Thread.sleep(60000);
// Show your ad here
}
}).start();