闪屏页添加广告
This commit is contained in:
parent
17441a0d78
commit
3bebde7111
|
|
@ -30,6 +30,7 @@
|
|||
android:name="com.gamedog.vididin.features.splash.SplashActivity"
|
||||
android:exported="true"
|
||||
android:launchMode="singleTop"
|
||||
android:theme="@style/Theme.App.TransparentSplash"
|
||||
android:screenOrientation="portrait">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
|
|
|||
|
|
@ -3,16 +3,19 @@ package com.gamedog.vididin.features.splash
|
|||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.view.LayoutInflater
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.core.view.updatePadding
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.ama.core.architecture.appBase.AppViewsEmptyViewModelActivity
|
||||
import com.gamedog.vididin.main.interfaces.OnTabStyleListener
|
||||
import com.gamedog.vididin.router.Router
|
||||
import com.remax.bill.ads.AdResult
|
||||
import com.remax.bill.ads.PreloadController
|
||||
import com.remax.bill.ads.bidding.AppOpenBiddingInitializer
|
||||
import com.remax.bill.ads.config.AdConfigManager
|
||||
import com.remax.bill.ads.ext.AdShowExt
|
||||
import com.remax.bill.ads.log.AdLogger
|
||||
import com.vididin.real.money.game.R
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import com.vididin.real.money.game.databinding.ActivitySplashBinding as ViewBinding
|
||||
|
|
@ -22,9 +25,14 @@ import com.vididin.real.money.game.databinding.ActivitySplashBinding as ViewBind
|
|||
@AndroidEntryPoint
|
||||
class SplashActivity : AppViewsEmptyViewModelActivity<ViewBinding>(), OnTabStyleListener {
|
||||
|
||||
private var isAdLoaded = false
|
||||
private var startTime = 0L
|
||||
|
||||
override fun inflateViewBinding(inflater: LayoutInflater) = ViewBinding.inflate(inflater)
|
||||
|
||||
override fun ViewBinding.initViews() {
|
||||
startTime = System.currentTimeMillis()
|
||||
|
||||
with(binding) {
|
||||
|
||||
}
|
||||
|
|
@ -37,6 +45,9 @@ class SplashActivity : AppViewsEmptyViewModelActivity<ViewBinding>(), OnTabStyle
|
|||
|
||||
override fun ViewBinding.initListeners() {
|
||||
startFallbackTimer()
|
||||
|
||||
// 初始化广告和应用
|
||||
initializeApp()
|
||||
}
|
||||
|
||||
override fun ViewBinding.initObservers() {
|
||||
|
|
@ -54,9 +65,125 @@ class SplashActivity : AppViewsEmptyViewModelActivity<ViewBinding>(), OnTabStyle
|
|||
}
|
||||
}
|
||||
|
||||
private fun initializeApp() {
|
||||
lifecycleScope.launch {
|
||||
try {
|
||||
async { performOtherInitializations() }
|
||||
val adMobInitDeferred = async { initializeAd() }
|
||||
|
||||
val adMobResult = adMobInitDeferred.await()
|
||||
|
||||
// 处理AdMob初始化结果
|
||||
when (adMobResult) {
|
||||
is AdResult.Success -> {
|
||||
// 2. 加载开屏广告
|
||||
showAppOpenAd()
|
||||
}
|
||||
|
||||
is AdResult.Failure -> {
|
||||
AdLogger.e("AdMob SDK初始化失败: ${adMobResult.error.message}")
|
||||
// 初始化失败,延迟后直接跳转
|
||||
delayAndJumpToMain()
|
||||
}
|
||||
|
||||
AdResult.Loading -> {
|
||||
AdLogger.d("AdMob SDK初始化中...")
|
||||
}
|
||||
}
|
||||
|
||||
} catch (e: Exception) {
|
||||
AdLogger.e("应用初始化异常", e)
|
||||
delayAndJumpToMain()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private suspend fun initializeAd(): AdResult<Unit> {
|
||||
return AppOpenBiddingInitializer.initialize(this@SplashActivity, R.mipmap.ic_launcher)
|
||||
}
|
||||
|
||||
private suspend fun performOtherInitializations() {
|
||||
for (i in 0..100 step 1) {
|
||||
// 检测到广告已显示,立即设置进度为100
|
||||
if (isAdLoaded) {
|
||||
//selfBindView.progressBar.progress = 100
|
||||
break
|
||||
}
|
||||
//selfBindView.progressBar.progress = i
|
||||
delay(100) // 改为100毫秒,总共10秒完成动画
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private suspend fun showAppOpenAd() {
|
||||
if (isAdLoaded) {
|
||||
return
|
||||
}
|
||||
|
||||
when (val result = AdShowExt.showAppOpenAd(this@SplashActivity) { loaded ->
|
||||
isAdLoaded = loaded
|
||||
PreloadController.preload(this)
|
||||
PreloadController.preloadPangle(this)
|
||||
PreloadController.preloadTopOn(this)
|
||||
/*DataReportManager.reportData(
|
||||
"loading_page_end", mapOf(
|
||||
"pass_time" to ceil((System.currentTimeMillis() - startTime) / 1000.0).toInt()
|
||||
)
|
||||
)*/
|
||||
}) {
|
||||
is AdResult.Success -> {
|
||||
delayAndJumpToMain()
|
||||
}
|
||||
|
||||
is AdResult.Failure -> {
|
||||
AdLogger.e("开屏广告显示失败: ${result.error.message}")
|
||||
// 开屏失败尝试展示插页
|
||||
if (AdConfigManager.shouldShowInterstitialAfterAppOpenFailure()) {
|
||||
lifecycleScope.launch {
|
||||
try {
|
||||
when (val interstitialResult = AdShowExt.showInterstitialAd(this@SplashActivity)) {
|
||||
is AdResult.Success -> {
|
||||
delayAndJumpToMain()
|
||||
}
|
||||
|
||||
is AdResult.Failure -> {
|
||||
delayAndJumpToMain()
|
||||
}
|
||||
|
||||
AdResult.Loading -> {
|
||||
}
|
||||
}
|
||||
|
||||
} catch (e: Exception) {
|
||||
delayAndJumpToMain()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
delayAndJumpToMain()
|
||||
}
|
||||
}
|
||||
|
||||
AdResult.Loading -> {
|
||||
AdLogger.d("开屏广告显示中...")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun delayAndJumpToMain() {
|
||||
val elapsedTime = System.currentTimeMillis() - startTime
|
||||
val remainingTime = MIN_SPLASH_TIME - elapsedTime
|
||||
|
||||
if (remainingTime > 0) {
|
||||
delay(remainingTime)
|
||||
}
|
||||
|
||||
Router.Main.startActivity(this@SplashActivity)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val MAX_SPLASH_TIME = 5 * 1000L
|
||||
const val MAX_SPLASH_TIME = 10 * 1000L
|
||||
const val MIN_SPLASH_TIME = 2 * 1000L
|
||||
|
||||
internal fun startActivity(activity: Activity) {
|
||||
activity.startActivity(Intent(activity.applicationContext, SplashActivity::class.java))
|
||||
|
|
|
|||
|
|
@ -6,4 +6,10 @@
|
|||
<item name="android:textStyle">bold</item>
|
||||
</style>
|
||||
|
||||
<style name="Theme.App.TransparentSplash" parent="Theme.AppCompat.Light.NoActionBar">
|
||||
<item name="android:windowBackground">@android:color/transparent</item>
|
||||
<item name="android:statusBarColor">@android:color/transparent</item>
|
||||
<item name="android:windowFullscreen">true</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
Loading…
Reference in New Issue