如何在x操作后加载非页内广告

时间:2017-03-04 20:28:19

标签: 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);
    }

1 个答案:

答案 0 :(得分:2)

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

private static int x = 0;

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

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