How to Show Interstitial Ads with Time Delay in Admob

How to show interstitial ads with time delay:

Interstitial ads are the highest eCPM ads that make good money to the AdMob publishers. Also, while showing the interstitial ads should not harm the user experience such as surprising ads, too many ads, un accidental clicks driving, etc. however, in this article we are going to learn about how to show the interstitial ads at a specific time.

Step1: Go to the android studio open your application.

Step2: Then Go to build: Gradle(: app), paste the below mentioned. This is the code is for loading the ads on your screen.

androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

Step3: Now Open your mainactivity.Java file and add the following java codes at the top. The below said code is used to define the classes for the interstitial ads.

import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.InterstitialAd;

import static com.google.android.gms.ads.AdRequest.*;

Step4: Now create a private variable or paste the below code inside the main public class MainActivity extends AppCompatActivity.

private InterstitialAd mInterstitialAd;
mInterstitialAd = new InterstitialAd(context: this);
    mInterstitialAd.setAdUnitId("ca-app-pub-4227838992174586/6645593425");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
    mInterstitialAd.setAdListener(new AdListener() {
        @Override public void onAdClosed()
        { mInterstitialAd.loadAd(new AdRequest.Builder().build()); } });
    Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            Intertialshow();
        }
    },  delayMillis: 20000 );

}
public void Intertialshow()
{ if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); }
else
{

} }

Look at the above delayMillis is the function used to define the time delay of showing the ads. Hereby default I have set it for 20000 milliseconds (20 seconds). You can increase the time delay of the ad load.

Also, change your ad code from ca-app-pub-4227838992174586/6645593425 to your code.

Step5: Now run the AVD. Then you see the test interstitial ad.

Note that, ads implementation should compile the AdSense program policy.

Leave a Comment