如何在 Kotlin Android 中重新加载 Interstitial Google 广告(新版本)
How to reload Interstitial Google Ads (New Version) in Kotlin Android
我在我的 Kotlin 项目中添加了插页式 Google 广告。它只加载一次,当我试图重新显示它时,它没有显示。
// Defined variable on top
private var mInterstitialAd: InterstitialAd? = null
MobileAds.initialize(requireContext()) {}
val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)
InterstitialAd.load(requireContext(),"---Interstitial Unit id---", adRequest, object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d("LoadAdTAG", adError?.message)
mInterstitialAd = null
}
override fun onAdLoaded(interstitialAd: InterstitialAd) {
Log.d("LoadAdTAG", "Ad was loaded.")
mInterstitialAd = interstitialAd
}
})
mInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
Log.d("LoadAdTAG", "Ad was dismissed.")
}
override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
Log.d("LoadAdTAG", "Ad failed to show.")
}
override fun onAdShowedFullScreenContent() {
Log.d("LoadAdTAG", "Ad showed fullscreen content.")
// mInterstitialAd = null
}
}
然后我在一段时间后以某种条件展示它。第一次显示如下
mInterstitialAd?.show(requireActivity())
但下次尝试显示时,会调用此行但广告未显示。
你能帮我看看如何再次展示广告吗?
我已关注以下 Google 文档(新版本):https://developers.google.com/admob/android/interstitial
添加此代码:-
fun loadAds() {
val adRequest = AdRequest.Builder().build()
InterstitialAd.load(requireActivity(), interstitial_id, adRequest, object : InterstitialAdLoadCallback() {
override fun onAdLoaded(interstitialAds: InterstitialAd) {
mInterstitialAd = interstitialAds
mInterstitialAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
loadAds()
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {}
override fun onAdShowedFullScreenContent() {
mInterstitialAd = null
}
}
}
override fun onAdFailedToLoad(loadAdError: LoadAdError) {
mInterstitialAd = null
}
})
}
为展示广告添加此代码:-
if (mInterstitialAd != null) {
mInterstitialAd!!.show(requireActivity())
}