运行 来自 SliderLayout 的插页式广告
Running Interstitial ad from SliderLayout
我的应用程序中有一个插页式广告,当我在 onCreate()
中显示该广告时它有效。
现在我正在使用一个库来制作带有 Glide 库的图像滑块,我想在 20 个图像滚动后显示广告。
我使用计数器来确定滚动的图像数量,然后显示广告。
这是Interstitial和AdRequest的初始化和创建
InterstitialAd interstitial;
AdRequest adRequest;
SliderLayout slider;
int count=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slider);
slider = findViewById(R.id.slider);
interstitial = new InterstitialAd(SliderActivity.this);
interstitial.setAdUnitId(getString(R.string.ad_interisial));
adRequest = new AdRequest.Builder().addTestDevice("7B7D134FF9931565B8C442934E12A0C1").build();
interstitial.loadAd(adRequest);
}
滑块为滚动页面提供监听器,这里是我写的代码
slider.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
@Override
public void onPageSelected(int i) {
count++;
System.out.println("count = " + count);
if(count %20 == 0){
System.out.println("twenty times");
displayInterstitial();
}
}
});
这是displayInterstitial()
方法
public void displayInterstitial() {
if (interstitial.isLoaded())
{
interstitial.show();
}
}
当我滚动 20 张图片时,代码打印 "twenty times",但未加载广告。
我错过了什么吗?
为什么会出现这个问题?
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
System.out.println("adLoaded");
interstitial.show();
}
});
您需要在 onCreate()
方法中添加此侦听器。
并更改方法 displayInterstitial()
的内容,如下所示:
public void displayInterstitial() {
if (!interstitial.isLoaded())
{
interstitial.loadAd(adRequest);
}
}
这是因为在寻呼机计数到 20 之前加载了广告。
当您调用 interstitial.loadAd(adRequest);
时,一段时间后您可以通过打印日志看到 onLoaded 调用了这个并且 if(count %20 == 0)
return false。
解决方案
您必须在调用 loadAd
之前设置广告加载侦听器。
interstitial.setAdListener(new AdListener()
{
public void onAdLoaded()
{
System.out.println("adLoaded");
isAdLoaded = true;
}
}
interstitial.loadAd(adRequest);
并在 onPageSelected
中展示广告
@Override
public void onPageSelected(int i) {
count++;
System.out.println("count = " + count);
if(isAdLoaded && count%20 == 0){
isAdLoaded = false;
System.out.println("twenty times");
displayInterstitial();
}
}
希望这对你有用。
我的应用程序中有一个插页式广告,当我在 onCreate()
中显示该广告时它有效。
现在我正在使用一个库来制作带有 Glide 库的图像滑块,我想在 20 个图像滚动后显示广告。
我使用计数器来确定滚动的图像数量,然后显示广告。
这是Interstitial和AdRequest的初始化和创建
InterstitialAd interstitial;
AdRequest adRequest;
SliderLayout slider;
int count=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slider);
slider = findViewById(R.id.slider);
interstitial = new InterstitialAd(SliderActivity.this);
interstitial.setAdUnitId(getString(R.string.ad_interisial));
adRequest = new AdRequest.Builder().addTestDevice("7B7D134FF9931565B8C442934E12A0C1").build();
interstitial.loadAd(adRequest);
}
滑块为滚动页面提供监听器,这里是我写的代码
slider.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
@Override
public void onPageSelected(int i) {
count++;
System.out.println("count = " + count);
if(count %20 == 0){
System.out.println("twenty times");
displayInterstitial();
}
}
});
这是displayInterstitial()
方法
public void displayInterstitial() {
if (interstitial.isLoaded())
{
interstitial.show();
}
}
当我滚动 20 张图片时,代码打印 "twenty times",但未加载广告。
我错过了什么吗? 为什么会出现这个问题?
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
System.out.println("adLoaded");
interstitial.show();
}
});
您需要在 onCreate()
方法中添加此侦听器。
并更改方法 displayInterstitial()
的内容,如下所示:
public void displayInterstitial() {
if (!interstitial.isLoaded())
{
interstitial.loadAd(adRequest);
}
}
这是因为在寻呼机计数到 20 之前加载了广告。
当您调用 interstitial.loadAd(adRequest);
时,一段时间后您可以通过打印日志看到 onLoaded 调用了这个并且 if(count %20 == 0)
return false。
解决方案
您必须在调用 loadAd
之前设置广告加载侦听器。
interstitial.setAdListener(new AdListener()
{
public void onAdLoaded()
{
System.out.println("adLoaded");
isAdLoaded = true;
}
}
interstitial.loadAd(adRequest);
并在 onPageSelected
@Override
public void onPageSelected(int i) {
count++;
System.out.println("count = " + count);
if(isAdLoaded && count%20 == 0){
isAdLoaded = false;
System.out.println("twenty times");
displayInterstitial();
}
}
希望这对你有用。