如何调用多个插屏广告?
How to call multiple interstitial ad?
我在包含一个 activity 的应用程序中添加了插页式广告(+横幅),横幅没有问题,但插页式广告仅在应用程序启动时显示一次,我的应用程序不包含任何按钮或actions 只是一个 pdfview,我想做的是在用户阅读 pdf 时每 5 分钟显示一次广告。我搜索了类似的问题,但找不到。这是我加载和显示广告的代码:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(PDFViewActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
我是这样做的:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
public void onAdClosed() {
requestNewInterstitial();
}
});
}
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
public void requestNewInterstitial() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
AdRequest adRequest = new AdRequest.Builder()
.build();
interstitial.loadAd(adRequest);
if (mHandler != null) {
mHandler.postDelayed(this, 100000); //time (ms)
}
}
}, 100000); //time (ms)
}
好的,您正在尝试定期显示插页式广告。好吧,您可以使用下面的代码行来完成。
但是,它违反了 Google admob 政策。但是给你。
在mainactiviy.java中添加以下代码。
prepareAd();
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
Log.i("hello", "world");
runOnUiThread(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG"," Interstitial not loaded");
}
prepareAd();
}
});
}
}, 20, 20, TimeUnit.SECONDS);
然后在 MainActivity.java
的最后一个大括号之前添加以下代码行
public void prepareAd(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-39xxx560xxxxxxx/10331xxxxx");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
public void prepareAd(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-39xxx560xxxxxxx/10331xxxxx");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
此处使用的插页式 AdMob ID 是示例 ID。代码取自 http://www.elegantespace.com/how-to-load-interstitial-ad-at-regular-interval-of-time/
我在包含一个 activity 的应用程序中添加了插页式广告(+横幅),横幅没有问题,但插页式广告仅在应用程序启动时显示一次,我的应用程序不包含任何按钮或actions 只是一个 pdfview,我想做的是在用户阅读 pdf 时每 5 分钟显示一次广告。我搜索了类似的问题,但找不到。这是我加载和显示广告的代码:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(PDFViewActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
我是这样做的:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
public void onAdClosed() {
requestNewInterstitial();
}
});
}
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
public void requestNewInterstitial() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
AdRequest adRequest = new AdRequest.Builder()
.build();
interstitial.loadAd(adRequest);
if (mHandler != null) {
mHandler.postDelayed(this, 100000); //time (ms)
}
}
}, 100000); //time (ms)
}
好的,您正在尝试定期显示插页式广告。好吧,您可以使用下面的代码行来完成。
但是,它违反了 Google admob 政策。但是给你。
在mainactiviy.java中添加以下代码。
prepareAd();
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
Log.i("hello", "world");
runOnUiThread(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG"," Interstitial not loaded");
}
prepareAd();
}
});
}
}, 20, 20, TimeUnit.SECONDS);
然后在 MainActivity.java
的最后一个大括号之前添加以下代码行public void prepareAd(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-39xxx560xxxxxxx/10331xxxxx");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
public void prepareAd(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-39xxx560xxxxxxx/10331xxxxx");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
此处使用的插页式 AdMob ID 是示例 ID。代码取自 http://www.elegantespace.com/how-to-load-interstitial-ad-at-regular-interval-of-time/