diff --git a/.idea/emulatorDisplays.xml b/.idea/emulatorDisplays.xml deleted file mode 100644 index e6b4ce5..0000000 --- a/.idea/emulatorDisplays.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 12ad6ee..16039ae 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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) @@ -72,4 +71,4 @@ dependencies { androidTestImplementation(libs.androidx.ui.test.junit4) debugImplementation(libs.androidx.ui.tooling) debugImplementation(libs.androidx.ui.test.manifest) -} \ No newline at end of file +} diff --git a/app/src/main/java/io/sixminutes/breakingnews/ActivityManager.kt b/app/src/main/java/io/sixminutes/breakingnews/ActivityManager.kt new file mode 100644 index 0000000..d278a2a --- /dev/null +++ b/app/src/main/java/io/sixminutes/breakingnews/ActivityManager.kt @@ -0,0 +1,22 @@ +package io.sixminutes.breakingnews + +import android.app.Activity + +object ActivityManager { + private val activities = mutableListOf() + + fun addActivity(activity: Activity) { + activities.add(activity) + } + + fun removeActivity(activity: Activity) { + activities.remove(activity) + } + + fun finishAll() { + for (activity in activities) { + activity.finish() + } + activities.clear() + } +} \ No newline at end of file diff --git a/app/src/main/java/io/sixminutes/breakingnews/BreakingnewApplication.kt b/app/src/main/java/io/sixminutes/breakingnews/BreakingnewApplication.kt index 1864766..0582428 100644 --- a/app/src/main/java/io/sixminutes/breakingnews/BreakingnewApplication.kt +++ b/app/src/main/java/io/sixminutes/breakingnews/BreakingnewApplication.kt @@ -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) {} } } \ No newline at end of file diff --git a/app/src/main/java/io/sixminutes/breakingnews/InterstitialActivity.kt b/app/src/main/java/io/sixminutes/breakingnews/InterstitialActivity.kt index 9b6e9e8..e70061e 100644 --- a/app/src/main/java/io/sixminutes/breakingnews/InterstitialActivity.kt +++ b/app/src/main/java/io/sixminutes/breakingnews/InterstitialActivity.kt @@ -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 } diff --git a/app/src/main/java/io/sixminutes/breakingnews/MainActivity.kt b/app/src/main/java/io/sixminutes/breakingnews/MainActivity.kt index 224f027..503812a 100644 --- a/app/src/main/java/io/sixminutes/breakingnews/MainActivity.kt +++ b/app/src/main/java/io/sixminutes/breakingnews/MainActivity.kt @@ -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,62 +46,42 @@ 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) + } + ) } } } } } - @Composable - fun MainContent() { - val deviceModel = Build.MODEL - 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) - } + 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( @@ -105,11 +90,68 @@ class MainActivity : ComponentActivity() { .padding(innerPadding), 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) + } } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b86e5d9..82fdecc 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" }