如何在Kotlin中每20秒展示一次插页式广告?
我使用此步骤在20秒后加载广告 我想每20秒在我的应用中加载一次广告 请 帮助
//ads
// Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713")
mAdView = findViewById(R.id.adView)
val adRequest = AdRequest.Builder().build()
mAdView.loadAd(adRequest)
mInterstitialAd = InterstitialAd(this)
mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
mInterstitialAd.loadAd(AdRequest.Builder().build())
val adsHandler = object : Handler(Looper.getMainLooper()){
override fun handleMessage(msg: Message?) {
mInterstitialAd.show()
}
}
fun scheduleAd(){
adsHandler.sendEmptyMessageDelayed(0, 20)
}
mInterstitialAd.setAdListener(object : AdListener() {
override fun onAdLoaded() {
scheduleAd()
}
override fun onAdClosed() {
// reschedule
scheduleAd()
}
})
fun loadAd() {
adsHandler.post {
mInterstitialAd.loadAd(AdRequest.Builder().build())
adsHandler.postDelayed(refresh, 20 * 1000) //called every 20 seconds
}
}
//ads over
答案 0 :(得分:0)
您可以使用
private val refresh = Runnable { loadAd() }
private fun loadAd() {
adsHandler.post {
mInterstitialAd.loadAd(AdRequest.Builder().build())
adsHandler.postDelayed(refresh, 20 * 1000) //called every 20 seconds
}
}
您可以使用loadAd()