How To Load Interstitial Ad At Regular Interval Of Time
In this post, I will guide you in placing Interstitial Ad in such a way that it will automatically start displaying Ads at the regular interval of time. You can set the time, after how many seconds the Ad should reappear. So, let’s get started.
Disclaimer: Please check AdMob policy before placing Interstitial Ad automatically. I am just showing how you can display Ad at regular interval of time. I am doing this because of the request I got from my website visitor.
⟩⟩ First of all, you need to add below line of code in your build.gradle dependencies file.
compile 'com.google.android.gms:play-services-ads:10.2.4'
⟩⟩ Now open the Manifest file and add Internet permission to it.
<uses-permission android:name="android.permission.INTERNET" />
⟩⟩  Now open MainActivity.java file and add only line number 3 in public class as shown below in bold.
public class MainActivity extends AppCompatActivity { //Add variable private InterstitialAd mInterstitialAd; }
⟩⟩  In the same file i.e MainActivity.java add below lines of code in onCreate() method
prepareAd(); ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Runnable() { public void run() { Log.i("hello", "world"); runOnUiThread(new Runnable() { public void run() { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } else { Log.d("TAG"," Interstitial not loaded"); } prepareAd(); } }); } }, 20, 20, TimeUnit.SECONDS);
You can change 20 seconds to 30 seconds or whatever you want it depends on you.
⟩⟩  Then add below lines of code before the last curly bracket of MainActivity.javaÂ
public void prepareAd(){ mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitId("ca-app-pub-39xxx560xxxxxxx/10331xxxxx"); mInterstitialAd.loadAd(new AdRequest.Builder().build()); }
Interstitial AdMob ID used here is a sample ID. You need to replace this test ID with your own Interstitial test Ad ID.
If you don’t know how to get your own Interstitial Ad Id then check this guide on how to get Interstitial Ad Unit ID.
Note: Don’t use your real Ad Unit ID while testing and never click on your own Ads it is against AdMob Policy.
That’s it, friends, this is the correct and simple way to Load Interstitial Ad At Regular Interval Of Time in your Application. Just follow the above steps and copy-paste the code carefully at the correct location mentioned above.
If you are a beginner you will love to learn Android Application development from scratch.
If you found this post helpful please do comment below and share the post to support me. If you have any suggestion please feel free to comment below.
Perfect, you just made my day.
I am glad. Hope you will like other posts too. 🙂