unarea
unarea

Reputation: 43

How to show Interstitial ads after x seconds on app

I set an insterstitital add that shows inmediatelly when the app is opened but I would like it to appear lets say after a minute.

Here is the part of my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    interstitial = new InterstitialAd(MainActivity.this);
    interstitial.setAdUnitId("ca-app-pub-XXXXXXXXXXXXXXXXXXXXXXXXXX");


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

    mAdView.loadAd(adRequest);

    interstitial.loadAd(adRequest);

    // Bind to the service
    try {
        bindIntent = new Intent(this, RadioService.class);
        bindService(bindIntent, radioConnection, Context.BIND_AUTO_CREATE);
    } catch (Exception e) {

    }

    telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (telephonyManager != null) {
        telephonyManager.listen(phoneStateListener,
                PhoneStateListener.LISTEN_CALL_STATE);
    }

    handler = new Handler();
    initialize();

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

Help is welcome!!

Thank you in advance!

Upvotes: 0

Views: 3503

Answers (1)

JAAD
JAAD

Reputation: 12379

you can use a Handler for that:

Handler handler = new Handler();


     handler.postDelayed(new Runnable(){

                @Override
                public void run() {
      displayInterstitial();
                }
            },60000);

   @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

Here 60000 is time in ms

so if you need for 20 mins then 60000*20

Upvotes: 4

Related Questions