Félix Maroy
Félix Maroy

Reputation: 1487

How to show Interstitial ads after X time in Kotlin?

My question is direct: "How can I show Interstitial ads in my kotlin app after X time ?" For exemple, the Interstitial ad must be shown automatically after 20 seconds. Which code can I use ? I did all tricks in this link https://developers.google.com/admob/android/interstitial but there is no solution for my problem.

Upvotes: 0

Views: 785

Answers (1)

Akaki Kapanadze
Akaki Kapanadze

Reputation: 2672

Use Handler if you need to schedule an event in the future. For example:

private val adsHandler = object : Handler(Looper.getMainLooper()){
    override fun handleMessage(msg: Message?) {
        interstitialAd.show()
    }
}

private fun scheduleAd(){
    adsHandler.sendEmptyMessageDelayed(0, 20_000)
}

and

...
interstitialAd.setAdListener(object : AdListener() {
            fun onAdLoaded() {
                scheduleAd()
            }

            fun onAdClosed() {
                // reschedule
                scheduleAd()
            }
        })

Upvotes: 1

Related Questions