How to load Interstitial ad after x actions(如何在 x 次操作后加载插页式广告)
问题描述
我正在编辑现有 android 应用程序(不是游戏)的代码.我想在经过一定数量的屏幕或操作后展示插页式广告(尊重 Google 指导).
I am editing the code of an existing android application (not a game). I want to show an interstitial ad after a certain number of screens or actions taken (Respecting Google guidance).
这是我通常加载插页式广告的方式:
This is how I normally load my interstitial ad:
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(News_Detail.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
AdRequest adRequest = new AdRequest.Builder().build();
// Load ads into Interstitial Ads
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.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Your code to show add
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}, 20000);
}
推荐答案
使用 int 跟踪操作,创建一个递增它的方法,并检查是否应该展示广告.如果是,请展示一个广告并重置它.
Keep track of actions using an int, make a method that increments it, and checks if you should show an ad. If yes, show an ad and reset it.
private static int x = 0;
public static void incrementActions() {
x++;
if(x >= 5) {
x = 0;
displayInterstital();
}
}
例如,将它放在您的 MainActivity 中,并像这样调用它:MainActivity.incrementActions().还要让你的 displayInterstital() 方法静态,或者给方法一个它所在类的对象.
Put it in your MainActivity for example, and call it like this: MainActivity.incrementActions().
Also make your displayInterstital() method static, or give the method an Object of the class it is in.
这篇关于如何在 x 次操作后加载插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 x 次操作后加载插页式广告
基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
