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 this
You can use a simple
Handler
to delay a function like this:(Kotlin version, but you get the idea)
Use this function like
reloadInterstitialAd()
in theonAdFailedToLoad
callback.I tried to do this.
Hope this fixes your problem.