GIT 提交信息:
重构活动管理并清理广告集成 - 通过将 SDK 初始化直接集成到应用程序的 `onCreate` 方法中,并移除过时的代码,进行重构。 - 更新应用程序中的日志记录和活动管理,以简化生命周期跟踪并改进调试。 - 简化 `MainContent` 组件,通过接受参数改善 UI 管理,从而增强模块化并减少冗余。 - 移除不必要的依赖项和配置文件,以清理项目结构。
This commit is contained in:
parent
c622b7e435
commit
7f6734670e
|
@ -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>
|
|
|
@ -64,7 +64,6 @@ dependencies {
|
||||||
implementation("com.applovin:applovin-sdk:+")
|
implementation("com.applovin:applovin-sdk:+")
|
||||||
implementation("com.applovin.dsp:linkedin-adapter:+")
|
implementation("com.applovin.dsp:linkedin-adapter:+")
|
||||||
implementation("com.adjust.sdk:adjust-android:5.0.2")
|
implementation("com.adjust.sdk:adjust-android:5.0.2")
|
||||||
implementation(libs.mediation.test.suite)
|
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
androidTestImplementation(libs.androidx.junit)
|
androidTestImplementation(libs.androidx.junit)
|
||||||
androidTestImplementation(libs.androidx.espresso.core)
|
androidTestImplementation(libs.androidx.espresso.core)
|
||||||
|
@ -72,4 +71,4 @@ dependencies {
|
||||||
androidTestImplementation(libs.androidx.ui.test.junit4)
|
androidTestImplementation(libs.androidx.ui.test.junit4)
|
||||||
debugImplementation(libs.androidx.ui.tooling)
|
debugImplementation(libs.androidx.ui.tooling)
|
||||||
debugImplementation(libs.androidx.ui.test.manifest)
|
debugImplementation(libs.androidx.ui.test.manifest)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,19 +11,12 @@ class BreakingNewsApplication : Application() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
|
||||||
// 初始化 AppLovin SDK
|
// 初始化 AppLovin SDK
|
||||||
initializeAppLovinSdk()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun initializeAppLovinSdk() {
|
|
||||||
val initConfig = AppLovinSdkInitializationConfiguration.builder(
|
val initConfig = AppLovinSdkInitializationConfiguration.builder(
|
||||||
"HVX8TLpa1WRO82HgVgS2OB8BfnTc-RkUHGGQQkXosEXgFq7n-3miRRw3JRqZwOW6R42ek58PpT9TM_N-glWMgc",
|
"HVX8TLpa1WRO82HgVgS2OB8BfnTc-RkUHGGQQkXosEXgFq7n-3miRRw3JRqZwOW6R42ek58PpT9TM_N-glWMgc",
|
||||||
this
|
this
|
||||||
).setMediationProvider(AppLovinMediationProvider.MAX).build()
|
).setMediationProvider(AppLovinMediationProvider.MAX).build()
|
||||||
|
|
||||||
// Initialize the SDK with the configuration
|
// Initialize the SDK with the configuration
|
||||||
|
AppLovinSdk.getInstance(this).initialize(initConfig) {}
|
||||||
AppLovinSdk.getInstance(this).initialize(initConfig) {
|
|
||||||
// createBannerAd()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,6 +13,7 @@ import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
import kotlin.math.pow
|
||||||
|
|
||||||
class InterstitialActivity : Activity(), MaxAdListener {
|
class InterstitialActivity : Activity(), MaxAdListener {
|
||||||
private lateinit var interstitialAd: MaxInterstitialAd
|
private lateinit var interstitialAd: MaxInterstitialAd
|
||||||
|
@ -20,20 +21,28 @@ class InterstitialActivity : Activity(), MaxAdListener {
|
||||||
private val maxRetryAttempts = 6 // 最大重试次数
|
private val maxRetryAttempts = 6 // 最大重试次数
|
||||||
private var adId: String? = null // 广告 ID
|
private var adId: String? = null // 广告 ID
|
||||||
private val scope = CoroutineScope(Dispatchers.Main) // 创建一个 CoroutineScope
|
private val scope = CoroutineScope(Dispatchers.Main) // 创建一个 CoroutineScope
|
||||||
private val TAG = "BreakingNews"
|
private val TAG = "breakingNews"
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
ActivityManager.addActivity(this)
|
||||||
|
|
||||||
// 获取从 MainActivity 传递过来的广告 ID
|
// 获取从 MainActivity 传递过来的广告 ID
|
||||||
adId = intent.getStringExtra("AD_ID")
|
adId = intent.getStringExtra("AD_ID")
|
||||||
if (adId != null) {
|
if (adId != null) {
|
||||||
createInterstitialAd(adId!!)
|
createInterstitialAd(adId!!)
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "Ad ID is null. Cannot load ad.")
|
Log.e(TAG, "Ad ID is null. Cannot load ad.")
|
||||||
|
setAdStatusResult("Ad ID is null. Cannot load ad.")
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
ActivityManager.removeActivity(this)
|
||||||
|
}
|
||||||
|
|
||||||
private fun createInterstitialAd(adId: String) {
|
private fun createInterstitialAd(adId: String) {
|
||||||
interstitialAd = MaxInterstitialAd(adId, this)
|
interstitialAd = MaxInterstitialAd(adId, this)
|
||||||
interstitialAd.setListener(this)
|
interstitialAd.setListener(this)
|
||||||
|
@ -47,7 +56,7 @@ class InterstitialActivity : Activity(), MaxAdListener {
|
||||||
|
|
||||||
// 显示广告
|
// 显示广告
|
||||||
if (interstitialAd.isReady) {
|
if (interstitialAd.isReady) {
|
||||||
interstitialAd.showAd()
|
interstitialAd.showAd(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回广告加载成功状态
|
// 返回广告加载成功状态
|
||||||
|
@ -59,7 +68,7 @@ class InterstitialActivity : Activity(), MaxAdListener {
|
||||||
|
|
||||||
if (retryAttempt < maxRetryAttempts) {
|
if (retryAttempt < maxRetryAttempts) {
|
||||||
retryAttempt++
|
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 {
|
scope.launch {
|
||||||
delay(delayMillis)
|
delay(delayMillis)
|
||||||
interstitialAd.loadAd()
|
interstitialAd.loadAd()
|
||||||
|
@ -86,7 +95,7 @@ class InterstitialActivity : Activity(), MaxAdListener {
|
||||||
|
|
||||||
override fun onAdHidden(maxAd: MaxAd) {
|
override fun onAdHidden(maxAd: MaxAd) {
|
||||||
Log.d(TAG, "Ad hidden: ${maxAd.adUnitId}")
|
Log.d(TAG, "Ad hidden: ${maxAd.adUnitId}")
|
||||||
interstitialAd.loadAd()
|
setAdStatusResult("Ad Hidden: ${maxAd.adUnitId}")
|
||||||
finish() // 广告关闭后结束当前 Activity
|
finish() // 广告关闭后结束当前 Activity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package io.sixminutes.breakingnews
|
||||||
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
@ -14,9 +15,9 @@ import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import io.sixminutes.breakingnews.ui.theme.BreakingnewsTheme
|
import io.sixminutes.breakingnews.ui.theme.BreakingnewsTheme
|
||||||
|
import kotlin.system.exitProcess
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
private val TAG = "BreakingNews"
|
private val TAG = "breakingNews"
|
||||||
|
|
||||||
// 定义 ActivityResultLauncher
|
// 定义 ActivityResultLauncher
|
||||||
private val interstitialLauncher = registerForActivityResult(
|
private val interstitialLauncher = registerForActivityResult(
|
||||||
|
@ -31,8 +32,12 @@ class MainActivity : ComponentActivity() {
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
ActivityManager.addActivity(this)
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
|
// 将 adStatus 提升到 setContent 作用域
|
||||||
|
var adStatus by remember { mutableStateOf("Ad Status: Unknown") }
|
||||||
|
|
||||||
BreakingnewsTheme {
|
BreakingnewsTheme {
|
||||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||||
Box(
|
Box(
|
||||||
|
@ -41,62 +46,42 @@ class MainActivity : ComponentActivity() {
|
||||||
.padding(innerPadding),
|
.padding(innerPadding),
|
||||||
contentAlignment = Alignment.Center
|
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)
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
override fun onDestroy() {
|
||||||
fun MainContent() {
|
super.onDestroy()
|
||||||
val deviceModel = Build.MODEL
|
ActivityManager.removeActivity(this)
|
||||||
var adStatus by remember { mutableStateOf("Ad Status: Unknown") }
|
|
||||||
|
|
||||||
Column(
|
|
||||||
modifier = Modifier.fillMaxSize(),
|
|
||||||
verticalArrangement = Arrangement.Center,
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
|
||||||
) {
|
|
||||||
Text(text = "Device Model: $deviceModel")
|
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
|
||||||
|
|
||||||
Button(onClick = {
|
|
||||||
// 调用 InterstitialAdManager 显示广告
|
|
||||||
InterstitialAdManager.showAd(this@MainActivity, "be20b7a9d66e8895", interstitialLauncher)
|
|
||||||
adStatus = "Ad 1 Loading..."
|
|
||||||
}) {
|
|
||||||
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..."
|
|
||||||
}) {
|
|
||||||
Text(text = "Show Inter Ad 2: be20b7a9d66e8896")
|
|
||||||
}
|
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
|
||||||
|
|
||||||
Button(onClick = {
|
|
||||||
finish()
|
|
||||||
}) {
|
|
||||||
Text(text = "Kill")
|
|
||||||
}
|
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
|
||||||
|
|
||||||
Text(text = adStatus)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateAdStatus(status: String) {
|
private fun updateAdStatus(status: String) {
|
||||||
// 更新 UI 中的广告状态
|
// 更新 UI 中的广告状态
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
setContent {
|
setContent {
|
||||||
|
var adStatus by remember { mutableStateOf(status) }
|
||||||
|
|
||||||
BreakingnewsTheme {
|
BreakingnewsTheme {
|
||||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||||
Box(
|
Box(
|
||||||
|
@ -105,11 +90,68 @@ class MainActivity : ComponentActivity() {
|
||||||
.padding(innerPadding),
|
.padding(innerPadding),
|
||||||
contentAlignment = Alignment.Center
|
contentAlignment = Alignment.Center
|
||||||
) {
|
) {
|
||||||
MainContent()
|
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)
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun MainContent(
|
||||||
|
adStatus: String,
|
||||||
|
onAdStatusUpdate: (String) -> Unit,
|
||||||
|
onShowAd1: () -> Unit,
|
||||||
|
onShowAd2: () -> Unit,
|
||||||
|
onKillApp: () -> Unit
|
||||||
|
) {
|
||||||
|
val deviceModel = Build.MODEL
|
||||||
|
|
||||||
|
Column(
|
||||||
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
verticalArrangement = Arrangement.Center,
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
|
) {
|
||||||
|
Text(text = "Device Model: $deviceModel")
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
Button(onClick = onShowAd1) {
|
||||||
|
Text(text = "Show Inter Ad 1: be20b7a9d66e8895")
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
Button(onClick = onShowAd2) {
|
||||||
|
Text(text = "Show Inter Ad 2: be20b7a9d66e8896")
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
Button(onClick = onKillApp) {
|
||||||
|
Text(text = "Kill")
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
Text(text = adStatus)
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
|
||||||
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
|
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
|
||||||
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
|
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
|
||||||
mediation-test-suite = { group = "com.google.android.ads", name = "mediation-test-suite", version.ref = "mediationTestSuite" }
|
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||||
|
|
Loading…
Reference in New Issue
Block a user