简体   繁体   English

如果已经加载了一个插页式广告,如何避免加载 Admob 插页式广告?

[英]How to Avoid Loading Admob Interstitial Ad if One is Already Loaded?

I am loading Interstitial ad in baseactivity's oncreate method, This baseactivity is extended by all other activities of the app so every time I start a new activity a new interstitial is being loaded even if there's one loaded already.我在 baseactivity 的 oncreate 方法中加载插页式广告,此 baseactivity 由应用程序的所有其他活动扩展,因此每次我开始一个新活动时,都会加载一个新的插页式广告,即使已经加载了一个。

private InterstitialAd mInterstitialAd;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createAd();
}

    public void createAd () {

        AdRequest adRequest = new AdRequest.Builder().build();

        InterstitialAd.load(BaseActivity.this, "ca-app-pub-3940256099942544/8691691433", adRequest, new InterstitialAdLoadCallback() {

            @Override
            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                Toast.makeText(BaseActivity.this, "Ad Loaded",
                        Toast.LENGTH_LONG).show();
                mInterstitialAd = interstitialAd;
                mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                    @Override
                    public void onAdDismissedFullScreenContent() {
                        // Called when fullscreen content is dismissed.
                        Toast.makeText(BaseActivity.this, "fullscreen content is dismissed.",
                                Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onAdFailedToShowFullScreenContent(AdError adError) {
                        // Called when fullscreen content failed to show.
                        Toast.makeText(BaseActivity.this, "fullscreen content failed to show.",
                                Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onAdShowedFullScreenContent() {
                        // Called when fullscreen content is shown.
                        // Make sure to set your reference to null so you don't
                        // show it a second time.
                        mInterstitialAd = null;
                        Toast.makeText(BaseActivity.this, "fullscreen content is shown.",
                                Toast.LENGTH_LONG).show();
                    }
                });
            }

            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                Toast.makeText(BaseActivity.this, "Ad Failed to Load",
                        Toast.LENGTH_LONG).show();
            }

        });

    }

How to load them only once when base activity created first time?第一次创建基本活动时如何只加载一次?

In you code every activity that extend base activity contains instance of it own of InterstitialAd , if you think that if you created base activity so InterstitialAd will be shared across all your activities you are wrong (read about inheritance).在您的代码中,扩展基本活动的每个活动都包含它自己的InterstitialAd实例,如果您认为如果您创建了基本活动,那么InterstitialAd将在您的所有活动中共享,那么您就错了(阅读关于继承)。

A quick hack is to make InterstitialAd static , a better approach is to create singleton class or that contain InterstitialAd , or even you can attach InterstitialAd to your application class, that by itself is singe instance. A quick hack is to make InterstitialAd static , a better approach is to create singleton class or that contain InterstitialAd , or even you can attach InterstitialAd to your application class, that by itself is singe instance.

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

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