【问题标题】:How to load Interstitial ad after x actions如何在 x 次操作后加载插页式广告
【发布时间】:2018-08-27 15:21:11
【问题描述】:

我正在编辑现有 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);
    }

【问题讨论】:

  • 我有一个测验应用程序。如果我在每 X 个问题(例如 15 个问题)之后展示一个插页式广告,Google 可以接受吗?

标签: java android admob


【解决方案1】:

使用int 跟踪操作,创建一个递增它的方法,并检查您是否应该展示广告。如果是,请展示一个广告并重置它。

private static int x = 0;

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

例如,将它放在您的 MainActivity 中,并像这样调用它:MainActivity.incrementActions()。 还要让你的 displayInterstital() 方法静态,或者给方法一个它所在类的对象。

【讨论】:

  • 谢谢,但是变量 x 不会改变他的值,如果我这样放这个函数!
  • x++ 等于 x = x + 1
  • 是的,它会增加它,我必须把这段代码放在哪里?
猜你喜欢
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多