java - 如何在 x 次操作后加载插页式广告

标签 java android admob

我正在编辑现有 Android 应用程序(不是游戏)的代码。我想在一定数量的屏幕或采取一定数量的操作后展示插页式广告(尊重 Google 指南)。

这是我通常加载插页式广告的方式:

// 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 跟踪操作,创建一个递增它的方法,并检查您是否应该显示广告。如果是,显示广告并重置它。

private static int x = 0;

public static void incrementActions() {
    x++;
    if(x >= 5) {
        x = 0;
        displayInterstital();
    }
}

例如将它放在您的 MainActivity 中,并像这样调用它:MainActivity.incrementActions()。 还要使您的 displayInterstital() 方法成为静态方法,或者为该方法提供它所在类的对象。

关于java - 如何在 x 次操作后加载插页式广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42601022/

相关文章:

java - 在 webview 中更改字体

android - 无法显示 AdMob 横幅

ios - AdMob Interstitial 每五场比赛一次

android - 以下类无法实例化 : - com. google.ads.AdView Admob

Java多重继承,摆脱重复代码

java - 使用目录创建 excel 文件 Spring boot java

android - getResources 和 String Array 强制关闭

javascript - react : onClick on Mobile (iPhone) requires 2 taps?

Java设置时区不默认为gmt+0

java - 检查对象是否为空