How to add some delay before requesting a new add when reloading the ad if it fails?

Viewed 23

I am trying to add a delay before reloading a new ad but I don´t know if I have implemented it well. I am using Looper.getMainLooper() to do this. You can see how I implemented the delay inside onAdFailedToLoad function. Also as you can see I have implemented that the ad if it fails it is also reloaded 5 times, but I also want to add a delay before the ad is loaded.

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){
                         new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                 @Override
                  public void run() {
                 mInterstitialAd = null;
                 requestNewInterstitial(maxRetry-1);
               }
              }, 10000);
                   
                    }
                });
                     }
           
    }




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

                        }

                    });
0 Answers
Related