我已经在我的应用程序中实现了插页式广告(admob),但是在我单击应用程序后不做任何操作之后,这些广告就会显示出来。
点击x后如何显示插页式广告,以及如何隐藏打开应用程序时出现的广告?
应用示例:https://drive.google.com/file/d/1x5vf6VY3mHu0S-aNnKeLQaq3FF-_9Xjx/view?usp=sharing
mainactivity.kt的一部分:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
hasCollections = boolean(R.bool.show_collections_tab)
val correct = if (hasCollections) 1 else 0
lastSection = savedInstanceState?.getInt("current", correct) ?: correct
setContentView(R.layout.activity_main)
// Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713")
mInterstitialAd = InterstitialAd(this)
mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
mInterstitialAd.loadAd(AdRequest.Builder().build())
mInterstitialAd.adListener = object: AdListener() {
override fun onAdLoaded() {
if (mInterstitialAd.isLoaded) {
mInterstitialAd.show()
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.")
}
}
}
setSupportActionBar(toolbar)
initPagerAdapter()
tabs?.setTabTextColors(
getDisabledTextColorFor(primaryColor),
if (boolean(R.bool.accent_in_tabs)) accentColor
else getPrimaryTextColorFor(primaryColor))
tabs?.setSelectedTabIndicatorColor(
if (boolean(R.bool.accent_in_tabs)) accentColor
else getPrimaryTextColorFor(primaryColor))
if (boolean(R.bool.show_icons_in_tabs)) {
tabs?.setTabsIconsColors(
getInactiveIconsColorFor(primaryColor),
if (boolean(R.bool.accent_in_tabs)) accentColor
else getActiveIconsColorFor(primaryColor))
}
buildTabs()
tabs?.addOnTabSelectedListener(
object : TabLayout.ViewPagerOnTabSelectedListener(pager) {
override fun onTabSelected(tab: TabLayout.Tab?) {
tab?.let { postDelayed(50) { navigateToSection(it.position) } }
}
override fun onTabReselected(tab: TabLayout.Tab?) = scrollToTop()
override fun onTabUnselected(tab: TabLayout.Tab?) {}
})
pager?.addOnPageChangeListener(
TabLayout.TabLayoutOnPageChangeListener(tabs))
pager?.offscreenPageLimit = tabs?.tabCount ?: 2
navigateToSection(lastSection, true)
favsViewModel.observe(this) { notifyFavsToFrags(it) }
doAsync { favsViewModel.loadData(favsDB.favoritesDao(), true) }
}
答案 0 :(得分:0)
您开始使用onCreate()
方法加载广告,该方法在启动MainActivity
之后立即调用。因此,您的回调方法onAdLoaded()
将在广告加载后立即显示。
如果您想稍后显示广告,则有两个选择:
loadAd()
,等待加载广告mInterstitialAd.show()
方法。这样,您的广告将被加载,但直到您需要时才显示。