在关闭应用之前添加AdMob插页式广告

时间:2016-12-26 11:22:55

标签: java android android-studio admob interstitial

我想在导航栏的退出链接上显示AdMob插页式广告,然后点击插页式广告或广告的关闭按钮关闭应用。

我正在使用以下代码,但它无法工作:

InterstitialAd mInterstitialAd;
    void loadAdsFullScreen(){
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad));
        AdRequest request = new AdRequest.Builder()
                .tagForChildDirectedTreatment(true)
                .build();
        // Load ads into Interstitial Ads
        mInterstitialAd.loadAd(request);

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial1();
            }
        });
    }

我用来关闭应用并展示广告的代码是:

else if (id == R.id.nav_exit) {

            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad));
            AdRequest request = new AdRequest.Builder()
                    .tagForChildDirectedTreatment(true)
                    .build();
            // Load ads into Interstitial Ads
            mInterstitialAd.loadAd(request);

            mInterstitialAd.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    showInterstitial();
                    finish();
                }
            });

            Test1.this.finish();
            System.exit(0);

        }

1 个答案:

答案 0 :(得分:0)

覆盖onAdClosed监听器,如

mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();

            }
             @Override
            public void onAdClosed() {
            Activity_Class_Name.this.finish();
        }

        @Override
        public void onAdOpened() {
            Activity_Class_Name.this.finish();
        }

        });