Set a delay of some seconds before reloading an interstitial ad

Viewed 48

I want to set/add a delay when reloading an ad when it has failed to load. I think it could be done with Looper.getMainLooper() but I don´t know how to do this. You can see I have implemented that if the ad fails to load,it will be reloaded up to 5 times, so I think that adding a delay before reloading the ad could be a good option.

Here is my code:

 @Override
      protected void onResume() {
        super.onResume();
        requestNewInterstitial(5);

        }




 private void requestNewInterstitial(int maxRetry) {
        AdRequest adRequest = new AdRequest.Builder().build();
        InterstitialAd.load(Activityone.this, getString(R.string.interid),
                adRequest, new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        mInterstitialAd = interstitialAd;
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {


          if (maxRetry>0){
                         
                  /*HERE IS WHERE THE AD IS RELOADED, SO THE DELAY COULD BE SET HERE BEFORE 
                 CALLING THE REQUEST NEW INTERSTITIAL FUNCTION*/
                 mInterstitialAd = null;
                 requestNewInterstitial(maxRetry-1);
              
                   
                    }
                });
                     }
           
    }




  btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            
                if (mInterstitialAd != null) {

                    mInterstitialAd.show(Activityone.this);

                    mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                        @Override
                        public void onAdDismissedFullScreenContent() {
                  
                        }

                        @Override
                        public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {

                        }

                    });
3 Answers

You can use a simple Handler to delay a function like this:
(Kotlin version, but you get the idea)

private fun reloadInterstitialAd() {
    if (maxRetry > 0) {
        val delayTime = 5000L // 5 seconds.
        val looper = mainLooper // Use `Looper.getMainLooper()` if you do not have access to an Activity context.

        mInterstitialAd = null
        Handler(looper).postDelayed({
            maxRetry--
            requestNewInterstitial(maxRetry)
        }, delayTime)
    } else {
        Log.d(TAG, "Max reload reached")
    }
}

Use this function like reloadInterstitialAd() in the onAdFailedToLoad callback.

I tried to do this.

if (maxRetry>0){
               
      //add a delay before retrying
      new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
              requestNewInterstitial(maxRetry-1);

          }
      }, 1000);
  }

Hope this fixes your problem.

You can use this

Thread.sleep(delay_time_in_milliseconds);
Related