GIT 提交信息:

重构活动管理并清理广告集成

- 通过将 SDK 初始化直接集成到应用程序的 `onCreate` 方法中,并移除过时的代码,进行重构。
- 更新应用程序中的日志记录和活动管理,以简化生命周期跟踪并改进调试。
- 简化 `MainContent` 组件,通过接受参数改善 UI 管理,从而增强模块化并减少冗余。
- 移除不必要的依赖项和配置文件,以清理项目结构。
This commit is contained in:
lvlisong 2025-01-24 04:33:57 +08:00
parent c622b7e435
commit 7f6734670e
7 changed files with 126 additions and 109 deletions

View File

@ -1,47 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EmulatorDisplays">
<option name="displayStateByAvdFolder">
<map>
<entry key="$USER_HOME$/.android/avd/Pixel_6_API_31.avd">
<value>
<MultiDisplayState>
<option name="displayDescriptors">
<list>
<DisplayDescriptor>
<option name="height" value="2553" />
<option name="width" value="1209" />
</DisplayDescriptor>
<DisplayDescriptor>
<option name="displayId" value="1" />
<option name="height" value="1920" />
<option name="width" value="1080" />
</DisplayDescriptor>
</list>
</option>
<option name="panelState">
<PanelState>
<option name="splitPanel">
<SplitPanelState>
<option name="proportion" value="0.9971671104431152" />
<option name="firstComponent">
<PanelState>
<option name="displayId" value="0" />
</PanelState>
</option>
<option name="secondComponent">
<PanelState>
<option name="displayId" value="1" />
</PanelState>
</option>
</SplitPanelState>
</option>
</PanelState>
</option>
</MultiDisplayState>
</value>
</entry>
</map>
</option>
</component>
</project>

View File

@ -64,7 +64,6 @@ dependencies {
implementation("com.applovin:applovin-sdk:+")
implementation("com.applovin.dsp:linkedin-adapter:+")
implementation("com.adjust.sdk:adjust-android:5.0.2")
implementation(libs.mediation.test.suite)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

View File

@ -0,0 +1,22 @@
package io.sixminutes.breakingnews
import android.app.Activity
object ActivityManager {
private val activities = mutableListOf<Activity>()
fun addActivity(activity: Activity) {
activities.add(activity)
}
fun removeActivity(activity: Activity) {
activities.remove(activity)
}
fun finishAll() {
for (activity in activities) {
activity.finish()
}
activities.clear()
}
}

View File

@ -11,19 +11,12 @@ class BreakingNewsApplication : Application() {
super.onCreate()
// 初始化 AppLovin SDK
initializeAppLovinSdk()
}
private fun initializeAppLovinSdk() {
val initConfig = AppLovinSdkInitializationConfiguration.builder(
"HVX8TLpa1WRO82HgVgS2OB8BfnTc-RkUHGGQQkXosEXgFq7n-3miRRw3JRqZwOW6R42ek58PpT9TM_N-glWMgc",
this
).setMediationProvider(AppLovinMediationProvider.MAX).build()
// Initialize the SDK with the configuration
AppLovinSdk.getInstance(this).initialize(initConfig) {
// createBannerAd()
}
AppLovinSdk.getInstance(this).initialize(initConfig) {}
}
}

View File

@ -13,6 +13,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit
import kotlin.math.pow
class InterstitialActivity : Activity(), MaxAdListener {
private lateinit var interstitialAd: MaxInterstitialAd
@ -20,20 +21,28 @@ class InterstitialActivity : Activity(), MaxAdListener {
private val maxRetryAttempts = 6 // 最大重试次数
private var adId: String? = null // 广告 ID
private val scope = CoroutineScope(Dispatchers.Main) // 创建一个 CoroutineScope
private val TAG = "BreakingNews"
private val TAG = "breakingNews"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ActivityManager.addActivity(this)
// 获取从 MainActivity 传递过来的广告 ID
adId = intent.getStringExtra("AD_ID")
if (adId != null) {
createInterstitialAd(adId!!)
} else {
Log.e(TAG, "Ad ID is null. Cannot load ad.")
setAdStatusResult("Ad ID is null. Cannot load ad.")
finish()
}
}
override fun onDestroy() {
super.onDestroy()
ActivityManager.removeActivity(this)
}
private fun createInterstitialAd(adId: String) {
interstitialAd = MaxInterstitialAd(adId, this)
interstitialAd.setListener(this)
@ -47,7 +56,7 @@ class InterstitialActivity : Activity(), MaxAdListener {
// 显示广告
if (interstitialAd.isReady) {
interstitialAd.showAd()
interstitialAd.showAd(this)
}
// 返回广告加载成功状态
@ -59,7 +68,7 @@ class InterstitialActivity : Activity(), MaxAdListener {
if (retryAttempt < maxRetryAttempts) {
retryAttempt++
val delayMillis = TimeUnit.SECONDS.toMillis(Math.pow(2.0, retryAttempt.toDouble()).toLong())
val delayMillis = TimeUnit.SECONDS.toMillis(2.0.pow(retryAttempt.toDouble()).toLong())
scope.launch {
delay(delayMillis)
interstitialAd.loadAd()
@ -86,7 +95,7 @@ class InterstitialActivity : Activity(), MaxAdListener {
override fun onAdHidden(maxAd: MaxAd) {
Log.d(TAG, "Ad hidden: ${maxAd.adUnitId}")
interstitialAd.loadAd()
setAdStatusResult("Ad Hidden: ${maxAd.adUnitId}")
finish() // 广告关闭后结束当前 Activity
}

View File

@ -2,6 +2,7 @@ package io.sixminutes.breakingnews
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
@ -14,9 +15,9 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.sixminutes.breakingnews.ui.theme.BreakingnewsTheme
import kotlin.system.exitProcess
class MainActivity : ComponentActivity() {
private val TAG = "BreakingNews"
private val TAG = "breakingNews"
// 定义 ActivityResultLauncher
private val interstitialLauncher = registerForActivityResult(
@ -31,8 +32,12 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ActivityManager.addActivity(this)
setContent {
// 将 adStatus 提升到 setContent 作用域
var adStatus by remember { mutableStateOf("Ad Status: Unknown") }
BreakingnewsTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Box(
@ -41,7 +46,70 @@ class MainActivity : ComponentActivity() {
.padding(innerPadding),
contentAlignment = Alignment.Center
) {
MainContent()
// 将 adStatus 作为参数传递给 MainContent
MainContent(
adStatus = adStatus,
onAdStatusUpdate = { newStatus ->
adStatus = newStatus
},
onShowAd1 = {
InterstitialAdManager.showAd(this@MainActivity, "be20b7a9d66e8895", interstitialLauncher)
adStatus = "Ad 1 Loading..."
},
onShowAd2 = {
InterstitialAdManager.showAd(this@MainActivity, "be20b7a9d66e8895", interstitialLauncher)
adStatus = "Ad 2 Loading..."
},
onKillApp = {
ActivityManager.finishAll()
exitProcess(0)
}
)
}
}
}
}
}
override fun onDestroy() {
super.onDestroy()
ActivityManager.removeActivity(this)
}
private fun updateAdStatus(status: String) {
// 更新 UI 中的广告状态
runOnUiThread {
setContent {
var adStatus by remember { mutableStateOf(status) }
BreakingnewsTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
contentAlignment = Alignment.Center
) {
MainContent(
adStatus = adStatus,
onAdStatusUpdate = { newStatus ->
adStatus = newStatus
},
onShowAd1 = {
InterstitialAdManager.showAd(this@MainActivity, "be20b7a9d66e8895", interstitialLauncher)
adStatus = "Ad 1 Loading..."
},
onShowAd2 = {
InterstitialAdManager.showAd(this@MainActivity, "be20b7a9d66e8896", interstitialLauncher)
adStatus = "Ad 2 Loading..."
},
onKillApp = {
ActivityManager.finishAll()
exitProcess(0)
}
)
}
}
}
}
}
@ -49,9 +117,14 @@ class MainActivity : ComponentActivity() {
}
@Composable
fun MainContent() {
fun MainContent(
adStatus: String,
onAdStatusUpdate: (String) -> Unit,
onShowAd1: () -> Unit,
onShowAd2: () -> Unit,
onKillApp: () -> Unit
) {
val deviceModel = Build.MODEL
var adStatus by remember { mutableStateOf("Ad Status: Unknown") }
Column(
modifier = Modifier.fillMaxSize(),
@ -61,29 +134,19 @@ class MainActivity : ComponentActivity() {
Text(text = "Device Model: $deviceModel")
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
// 调用 InterstitialAdManager 显示广告
InterstitialAdManager.showAd(this@MainActivity, "be20b7a9d66e8895", interstitialLauncher)
adStatus = "Ad 1 Loading..."
}) {
Button(onClick = onShowAd1) {
Text(text = "Show Inter Ad 1: be20b7a9d66e8895")
}
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
// 调用 InterstitialAdManager 显示广告
InterstitialAdManager.showAd(this@MainActivity, "be20b7a9d66e8895", interstitialLauncher)
adStatus = "Ad 2 Loading..."
}) {
Button(onClick = onShowAd2) {
Text(text = "Show Inter Ad 2: be20b7a9d66e8896")
}
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
finish()
}) {
Button(onClick = onKillApp) {
Text(text = "Kill")
}
@ -92,24 +155,3 @@ class MainActivity : ComponentActivity() {
Text(text = adStatus)
}
}
private fun updateAdStatus(status: String) {
// 更新 UI 中的广告状态
runOnUiThread {
setContent {
BreakingnewsTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
contentAlignment = Alignment.Center
) {
MainContent()
}
}
}
}
}
}
}

View File

@ -25,7 +25,6 @@ androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-toolin
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
mediation-test-suite = { group = "com.google.android.ads", name = "mediation-test-suite", version.ref = "mediationTestSuite" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }