如何解决如何解决插页式广告的问题?
打开应用程序后,SplashScreen 开启,其中 Interstitial Ads 开启, 关闭广告后转换到 MainActivity 发生,一切正常,但不知何故不正确,关闭广告后 屏幕变白 1-2 秒,然后才开始加载 MainActivity
SplashActivity
class SplashActivity : AppCompatActivity(),AdMobFullscreenManager.AdMobFullscreenDelegate {
internal var fullscreenManager: AdMobFullscreenManager? = null
private val adManager: AdMobFullscreenManager?
get() {
if (fullscreenManager == null) {
configureManager()
}
return fullscreenManager
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.splash_screen)
if (isOnline()) {
if (!SubscriptionProvider.hasSubscription()) {
adManager!!.completed()
} else {
Handler().postDelayed({
startActivity(Intent(this@SplashActivity,MainActivity::class.java))
},3000)
}
} else {
Handler().postDelayed({
startActivity(Intent(this@SplashActivity,MainActivity::class.java))
},3000)
}
}
private fun configureManager() {
if (fullscreenManager == null) {
fullscreenManager = AdMobFullscreenManager(this,this)
} else {
fullscreenManager!!.reloadAd()
}
}
override fun ADLoaded() {
if (adManager!!.tryingShowDone) {
adManager!!.showAdd()
} else {
startActivity(Intent(this@SplashActivity,MainActivity::class.java))
}
}
override fun ADIsClosed() {
if (adManager!!.tryingShowDone) {
val intent = Intent(this@SplashActivity,MainActivity::class.java)
startActivity(intent)
}
}
fun isOnline(): Boolean {
val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val netInfo = cm.activeNetworkInfo
return netInfo != null && netInfo.isConnectedOrConnecting
}
}
AdMobFullscreenManager
class AdMobFullscreenManager(private val context: Context?,delegate: AdMobFullscreenDelegate?) {
private var countRequestAd: Int = 0
private var delegate: AdMobFullscreenDelegate? = null
var tryingShowDone = false
private var adMobState = AdMobState.Loading
private lateinit var mInterstitialAd: InterstitialAd
enum class AdMobState {
Loaded,Loading,NoAd
}
init {
if (!SubscriptionProvider.hasSubscription()) {
mInterstitialAd = InterstitialAd(context)
configure(delegate)
}
}
private fun configure(delegate: AdMobFullscreenDelegate?) {
mInterstitialAd.setAdUnitId(context?.getString(R.string.interstitial)!!)
mInterstitialAd.loadAd(AdRequest.Builder().build())
this.delegate = delegate
mInterstitialAd.setAdListener(object : AdListener() {
override fun onAdLoaded() {
countRequestAd = 0
adMobState = AdMobState.Loaded
if (this@AdMobFullscreenManager.delegate != null) {
this@AdMobFullscreenManager.delegate!!.ADLoaded()
}
}
override fun onAdFailedToLoad(errorCode: Int) {
if (!SubscriptionProvider.hasSubscription()) {
this@AdMobFullscreenManager.reloadAd()
}
}
override fun onAdClosed() {
if (this@AdMobFullscreenManager.delegate != null) {
this@AdMobFullscreenManager.delegate!!.ADIsClosed()
}
}
})
}
fun showAdd(): Boolean {
var b = false
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show()
b = true
}
return b
}
fun completed() {
tryingShowDone = true
if (adMobState == AdMobState.Loaded) {
if (delegate != null) {
if (!SubscriptionProvider.hasSubscription()) {
delegate!!.ADLoaded()
}
}
} else if (adMobState == AdMobState.NoAd) {
if (delegate != null) {
delegate!!.ADIsClosed()
}
}
}
fun reloadAd() {
countRequestAd++
if (countRequestAd == MAX_REQUEST_AD) {
adMobState = AdMobState.NoAd
if (delegate != null) {
delegate!!.ADIsClosed()
}
return
}
if (mInterstitialAd.isLoaded()) {
adMobState = AdMobState.Loaded
mInterstitialAd.show()
} else if (!mInterstitialAd.isLoading()) {
adMobState = AdMobState.Loading
if (!SubscriptionProvider.hasSubscription()) {
mInterstitialAd.loadAd(AdRequest.Builder().build())
}
}
}
interface AdMobFullscreenDelegate {
fun ADLoaded()
fun ADIsClosed()
}
companion object {
private val MAX_REQUEST_AD = 3
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"
android:usesCleartextTraffic="true">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="" />
<activity
android:name=".SplashActivity"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:theme="@style/TranslucentTheme"
tools:replace="android:theme" />
</application>
</manifest>
应用
class App : Application() {
override fun onCreate() {
super.onCreate()
sInstance = this
MobileAds.initialize(this)
}
companion object {
private lateinit var sInstance: App
fun getInstance(): App {
return sInstance
}
fun getInstance(activity: Activity): App {
return activity.application as App
}
}
}
implementation 'com.google.android.gms:play-services-ads:19.5.0'
即使我让它变得容易很多次,例如
SplashActivity
class SplashActivity : AppCompatActivity() {
private lateinit var mInterstitialAd: InterstitialAd
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.splash_screen)
mInterstitialAd = InterstitialAd(this)
mInterstitialAd.setAdUnitId(this.getString(R.string.interstitial))
mInterstitialAd.loadAd(AdRequest.Builder().build())
mInterstitialAd.setAdListener(object : AdListener() {
override fun onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show()
}
}
override fun onAdClosed() {
val intent = Intent(this@SplashActivity,MainActivity::class.java)
startActivity(intent)
}
})
}
}
什么都没变,问题还是一样。 同时,应用程序中还有其他可以正常运行的插页式广告。 请告诉我可能出了什么问题以及如何解决?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。