简体   繁体   中英

Adding AdMob interstitial ads before closing the app

I want to display AdMob interstitial ad on exit link from the nav bar and close the app on click of either on the interstitial ad or the close button of the ad.

I am using the following codes but it won't work:

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

The code I am using for closing the app and showing the ads is:

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

        }

Override onAdClosed of listener like,

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

        });

I don't encourage you to do interstitial ads when exiting the app since google Admob privacy policy doesn't allow that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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