Home > Mobile >  Set a delay of some seconds before reloading an interstitial ad
Set a delay of some seconds before reloading an interstitial ad

Time:09-12

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

                        }

                    });

Thanks for your helping.

CodePudding user response:

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

if (maxRetry > 0) {

  val delayTime = 5000; // 5 seconds.
  val looper = mainLooper; // Use `Looper.getMainLooper()` if you do not have access to an Activity context.

  Handler(mainLooper).postDelayed( { 
      requestNewInterstitial(maxRetry - 1)
   }, delayTime)

}

CodePudding user response:

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.

CodePudding user response:

You can use this

Thread.sleep(delay_time_in_milliseconds);
  • Related