在按钮上单击Admob插页式广告

我使用kotlin在android studio中制作了一个应用程序,当点击一个按钮的广告出现时,我尝试添加插页式广告,并在广告关闭后使用onAdClosed()方法显示第二个活动,问题在于应用程序有时候会在主要活动2中保持压制状态3.这就是我的代码的样子

 private lateinit var mInterstitialAd: InterstitialAd private lateinit var adRequest : AdRequest override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713") mInterstitialAd = InterstitialAd(this) mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712" requestNewInterstitial() button2.setOnClickListener{ val intent1 = Intent(this,Main2Activity::class.java) startActivity(intent1) if (mInterstitialAd.isLoaded) { mInterstitialAd.show() } else { } mInterstitialAd.adListener = object : AdListener() { override fun onAdClosed() { requestNewInterstitial() //go to second activity or do something } override fun onAdLoaded() { requestNewInterstitial() } override fun onAdFailedToLoad(i: Int) { requestNewInterstitial() } } } } private fun requestNewInterstitial() { adRequest = AdRequest.Builder().build() mInterstitialAd.loadAd(adRequest)} 

我想当用户点击按钮时,应该看到广告,当他关闭广告,他应该去第二次活动

One Solution collect form web for “在按钮上单击Admob插页式广告”

尝试这个

 class MainActivity : AppCompatActivity() { lateinit var mInterstitialAd: InterstitialAd override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713") mInterstitialAd = InterstitialAd(this) mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712") mInterstitialAd.loadAd(AdRequest.Builder().build()) mInterstitialAd.adListener = object : AdListener() { override fun onAdClicked() { super.onAdClicked() } override fun onAdClosed() { val intent = Intent(applicationContext, SecondActivity::class.java) startActivity(intent) } } btn.setOnClickListener { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show() } else { Log.d("TAG", "The interstitial wasn't loaded yet.") } } } } 
  • Kotlin在variables声明之后分配委托
  • 如何添加一个新的配置使用Gradle脚本Kotlin?
  • 从资源字符串数组创建ArrayList
  • 如何在Kotlin中为每个数字types实现地板模数?
  • 在Kotlin中更改循环索引
  • Jersey CORS配置允许来自服务器的响应cookie
  • 如何创建Kotlin Annotation嵌套类
  • 在使用arrayOfNulls时,是否可以删除Kotlin中未经检查的转换?
  • 如何在kotlin中进行多条件循环
  • 在Kotlin中的正则expression式匹配
  • Retrofit2返回null kotlin中的单位为204否内容响应
  • Kotlin language will be the best programming language for Android.