简体   繁体   English

如何在整个App会话的活动中仅展示一次插页式广告

[英]How to show interstitial Ad only once in an activity throughout App session

(My problem is when i comeback to the same activity i dont want show again an ineterstitial Ad.I want to show interstitial Ad only once throught the app session.) currently i'm using the default interstitial Ad code given by google admob. (我的问题是,当我返回相同的活动时,我不想再次显示非常规广告。我只希望在应用会话中仅显示非常规广告。)目前,我使用的是Google admob提供的默认非常规广告代码。

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getString(R.string.interstitial_id));
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {

            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            }
                else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }


        }




    });

Just keep a static boolean variable. 只需保留一个静态布尔变量即可。 Once you load the app, make sure to set the boolean variable to false. 加载应用后,请确保将布尔变量设置为false。 Once the ad is loaded you can set it to true. 广告加载后,您可以将其设置为true。 When you exit the app make sure to set it back to false(since app process may not get destroyed as soon as you exit the app) 退出应用程序时,请确保将其设置回false(因为退出应用程序后应用程序进程可能不会被破坏)

    public static isAdLoadedOnce = false;
    ----------------------------------------------

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getString(R.string.interstitial_id));
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {

                if (!isAdLoadedOnce & mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                    isAdloadedOnce = true;
                }
                    else {
                    Log.d("TAG", "The interstitial wasn't loaded yet.");
                }
            }
        });

Hi Actually i have an patch for doing that,We will be Using sharedpreferences for that, 嗨,其实我有一个补丁可以做到这一点,我们将为此使用sharedpreferences,

  1. First thing you have to do is before loading ads just check in sharedpreferences that is ad is already been loaded once?.if not then just load/show ads and set flag to true. 您要做的第一件事是在加载广告之前,仅检查共享首选项是否已加载一次广告?如果不是,则只需加载/显示广告并将flag设置为true。

     SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean("isAdsLoaded", true); editor.commit(); 
  2. Okay so for Now whenever this activity open it will not load ads again but to load it again when app is killed and opened again we should have to reset flag. 好的,对于“现在”,无论何时打开此活动,它都不会再次加载广告,但是要在应用被终止并再次打开时再次加载广告,我们必须重置标志。
  3. To reset flag, In application class we will be setting this flag to false 要重置标志,在应用程序类中,我们将该标志设置为false

     SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean("isAdsLoaded", false); editor.commit(); 

Here Application class is the class which one be called every time you opened apps.I hope you know how to create it and mention in manifest file.and if you donn't please comment i will explain it in brief. 这里的Application类是每次您打开应用程序时都会被调用的类。我希望您知道如何创建它并在清单文件中提及。

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

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