简体   繁体   English

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

[英]How to load Interstitial ad after x actions

I am editing the code of an existing android application (not a game). 我正在编辑现有android应用程序(而不是游戏)的代码。 I want to show an interstitial ad after a certain number of screens or actions taken (Respecting Google guidance). 我想在执行一定数量的屏幕或操作后显示插页式广告(请参阅Google指南)。

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);
    }

Keep track of actions using an int , make a method that increments it, and checks if you should show an ad. 使用int跟踪动作,制作增加它的方法,并检查是否应该展示广告。 If yes, show an ad and reset it. 如果是,则显示广告并重置。

private static int x = 0;

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

Put it in your MainActivity for example, and call it like this: MainActivity.incrementActions() . 例如,将其放在您的MainActivity中,并按如下方式调用它: MainActivity.incrementActions() Also make your displayInterstital() method static, or give the method an Object of the class it is in. 还可以使displayInterstital()方法为静态方法,或为该方法提供其所属类的Object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM