如何在x次操作后加载非页内广告 [英] How to load Interstitial ad after x actions

查看:43
本文介绍了如何在x次操作后加载非页内广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑现有android应用程序(而不是游戏)的代码.我想在执行一定数量的屏幕或操作后显示插页式广告(请参阅Google指南).

I am editing the code of an existing android application (not a game). I want to show an interstitial ad after a certain number of screens or actions taken (Respecting Google guidance).

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

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

推荐答案

使用 int 跟踪动作,制作增加其效果的方法,并检查是否应展示广告.如果是,请显示广告并重置.

Keep track of actions using an int, make a method that increments it, and checks if you should show an ad. If yes, show an ad and reset it.

private static int x = 0;

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

例如,将其放入您的MainActivity中,并按以下方式调用它: MainActivity.incrementActions().还可以将您的 displayInterstital()方法设为静态,或为该方法提供其所在类的Object.

Put it in your MainActivity for example, and call it like this: MainActivity.incrementActions(). Also make your displayInterstital() method static, or give the method an Object of the class it is in.

这篇关于如何在x次操作后加载非页内广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆