107 lines
3.6 KiB
Kotlin
107 lines
3.6 KiB
Kotlin
package com.gogame.minesweeper
|
|
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.os.Handler
|
|
import android.os.Looper
|
|
import androidx.activity.enableEdgeToEdge
|
|
import androidx.core.view.ViewCompat
|
|
import androidx.core.view.WindowCompat
|
|
import androidx.core.view.WindowInsetsCompat
|
|
import com.gogame.minesweeper.ad.AdShowFailed
|
|
import com.gogame.minesweeper.ad.AdsInsUtil
|
|
import com.gogame.minesweeper.ad.LoadListener
|
|
import com.gogame.minesweeper.ad.ShowListener
|
|
import com.gogame.minesweeper.databinding.ActivitySplashBinding
|
|
import com.thinkup.core.api.TUAdInfo
|
|
import kotlin.random.Random
|
|
|
|
class SplashActivity : BaseActivity() {
|
|
|
|
companion object {
|
|
private const val AD_TIMEOUT = 15000L // 广告加载超时时间 15 秒
|
|
}
|
|
|
|
private lateinit var binding: ActivitySplashBinding
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
binding = ActivitySplashBinding.inflate(layoutInflater)
|
|
enableEdgeToEdge()
|
|
setContentView(binding.root)
|
|
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
|
|
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
|
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
|
insets
|
|
}
|
|
val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
|
|
windowInsetsController.isAppearanceLightStatusBars = true//状态栏文字颜色
|
|
loadSplashAd()
|
|
}
|
|
|
|
|
|
private val adHandler = Handler(Looper.getMainLooper())
|
|
private val adTimeoutRunnable = Runnable {
|
|
navigateToNext() // 超时直接跳转
|
|
}
|
|
|
|
private fun loadSplashAd() {
|
|
adHandler.postDelayed(adTimeoutRunnable, AD_TIMEOUT)
|
|
|
|
// 生成一个随机索引
|
|
val randomIndex = Random.nextInt(AdsInsUtil.Placement.adPlaceAllList.size)
|
|
// 获取随机的广告位
|
|
val randomAdPlace = AdsInsUtil.Placement.adPlaceAllList[randomIndex]
|
|
AdsInsUtil.loadAd(
|
|
act = this,
|
|
adID =randomAdPlace,
|
|
loadListener = object : LoadListener {
|
|
override fun loaded(ad: TUAdInfo) {
|
|
adHandler.removeCallbacks(adTimeoutRunnable)
|
|
showSplashAd(randomAdPlace)
|
|
}
|
|
|
|
override fun loadFailed(error: String) {
|
|
adHandler.removeCallbacks(adTimeoutRunnable)
|
|
navigateToNext() // 加载失败直接跳转
|
|
}
|
|
})
|
|
}
|
|
|
|
private fun showSplashAd(randomAdPlace: String) {
|
|
AdsInsUtil.showAd(
|
|
act = this,
|
|
adID = randomAdPlace,
|
|
listener = object : ShowListener {
|
|
override fun onAdShown(ad: TUAdInfo?) {
|
|
}
|
|
|
|
override fun onAdShowFailed(error: AdShowFailed?) {
|
|
navigateToNext() // 展示失败也直接跳转
|
|
}
|
|
|
|
override fun onAdClosed() {
|
|
navigateToNext() // 广告关闭后跳转
|
|
}
|
|
})
|
|
}
|
|
|
|
private var hasNavigated = false
|
|
|
|
//保证只跳转一次
|
|
private fun navigateToNext() {
|
|
if (hasNavigated) return
|
|
hasNavigated = true
|
|
startActivity(Intent(this, MainActivity::class.java))
|
|
finish()
|
|
}
|
|
|
|
override fun shouldInterceptBackPress(): Boolean = true
|
|
override fun onInterceptBackPressed() {
|
|
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
super.onDestroy()
|
|
adHandler.removeCallbacks(adTimeoutRunnable)
|
|
}
|
|
} |