Search code examples
androidadmobadsinterstitial

How to call multiple interstitial ad?


I added interstitial ad (+banner) to my app that contains one activity, no problem with banner but the interstitial ad shows only at app launch one time, my app don't contain any buttons or actions just a pdfview, what I want to do is showing the ad every 5mins when the user reading the pdf. I searched the similar questions but couldn't get it. here my code to load and show the ad :

    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    // Prepare the Interstitial Ad
    interstitial = new InterstitialAd(PDFViewActivity.this);
    // Insert the Ad Unit ID
    interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    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.
    if (interstitial.isLoaded()) {
        interstitial.show();
    }
}

Solution

  • I did it this way :

    AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    
        // Prepare the Interstitial Ad
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    
        interstitial.loadAd(adRequest);
    
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                displayInterstitial();
            }
            public void onAdClosed() {
                requestNewInterstitial();
            }
    
        });
    }
    
    public void displayInterstitial() {
    // If Ads are loaded, show Interstitial else show nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
    
    public void requestNewInterstitial() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                AdRequest adRequest = new AdRequest.Builder()
                        .build();
                interstitial.loadAd(adRequest);
    
                if (mHandler != null) {
                    mHandler.postDelayed(this, 100000); //time (ms)
                }
            }
        }, 100000); //time (ms)
    }