5

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();
    }
}
Mounir Lardjem
  • 127
  • 2
  • 7
  • Think about this idea twice. First of all, this is not the proposed way by Google to display interstitials. Second, you will piss off your users with such annoying behaviour, so they will never click on that and uninstall your app. – Bevor Feb 17 '17 at 18:30
  • If you think you know it better than your users, then good luck ;) – Bevor Feb 18 '17 at 07:57

2 Answers2

3

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)
}
Mounir Lardjem
  • 127
  • 2
  • 7
0

Okay so you are trying to display interstitial ad at regular interval of time. Well you can do it by using below line of code.

However, it is against Google admob policy. But here you go.

In mainactiviy.java add below codes.

prepareAd();

    ScheduledExecutorService scheduler =
            Executors.newSingleThreadScheduledExecutor();
    scheduler.scheduleAtFixedRate(new Runnable() {

        public void run() {
            Log.i("hello", "world");
            runOnUiThread(new Runnable() {
                public void run() {
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    } else {
                       Log.d("TAG"," Interstitial not loaded");
                    }

                    prepareAd();


                }
            });

        }
    }, 20, 20, TimeUnit.SECONDS);

Then add below lines of code before the last curly bracket of MainActivity.java

public void  prepareAd(){
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-39xxx560xxxxxxx/10331xxxxx");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
}

public void  prepareAd(){
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-39xxx560xxxxxxx/10331xxxxx");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
}

Interstitial AdMob ID used here is a sample ID. Codes taken from http://www.elegantespace.com/how-to-load-interstitial-ad-at-regular-interval-of-time/

Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47
Saurabh
  • 1
  • 2