如何在Kotlin停止之前在Android上启动的活动



我有两个应用程序。第一个应用程序启动第二个应用程序,并应在一段时间间隔(当前为10秒(后停止第二个程序,然后重新启动该应用程序。这是我迄今为止的代码:

package com.example.launch
import android.content.ActivityNotFoundException
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onResume() {
super.onResume()
val launchIntent = packageManager.getLaunchIntentForPackage("com.example.secondapp")
while (true) {
if (launchIntent != null) {
try {
startActivityForResult(launchIntent, 1)
Log.d("", "Launching com.example.secondapp")
} catch (e: ActivityNotFoundException) {
Log.d("", "Failed to launch com.example.secondapp")
}
} else {
Log.d("", "Intent is null value.")
}
Thread.sleep(10000)
finishActivity(1)
}
}
}

在我的安卓手机(Galaxy A12(上,第一个应用程序启动了第二个应用程序,但没有停止。以下是第一个应用的一些典型输出。

2022-10-28 00:17:58.127 21240-21240/com.example.launch D/: Launching com.example.secondapp
2022-10-28 00:18:08.140 21240-21240/com.example.launch D/: Launching com.example.secondapp
2022-10-28 00:18:18.150 21240-21240/com.example.launch D/: Launching com.example.secondapp

因此,从输出中可以看出,第一个应用程序相信它每10秒就会成功启动第二个应用程序。这与我在手机上或第二个应用程序的日志中看到的不一致,该应用程序正在继续运行,没有被停止或重新启动。

我哪里错了?我该怎么解决这个问题?

您应该制作第二个APP来决定何时结束。

如果第一个应用程序由于各种原因而过时,那么第二个应用程序将不会在时间结束时结束。

IMO,我会把时间传给第二个APP,让它决定什么时候结束。此外,我们将了解活动的生命周期。当APP进入后台时暂停计时器,当APP返回前台时重新启动计时器。

在第一个APP中,

class FirstActivity: AppCompatActivity() {
override fun onResume() {
super.onResume()
val launchIntent: Intent? = packageManager.getLaunchIntentForPackage("com.example.secondapp")

// added
val timeout = 10 *1000
launchIntent?.putExtra("timeout", timeout);
if (launchIntent != null) {
startActivity(launchIntent)
}
}

在第二个APP中,

class SecondActivity: AppCompatActivity() {
private val TAG = "SecondActivity"
private val activityLaunchTime = SystemClock.elapsedRealtime() // Time since last reboot
private var timeout = -1L // -1 -> null -> indicates it shall not apply timeout
private var timer: CountDownTimer? = null
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
timeout = intent.getLongExtra("timeout", -1);
Log.d(TAG, "onCreate: timeout(from intent extras)=$timeout")
}
override fun onResume() {
super.onResume()
// Pseudo code for explanation
// e.g.1, 00:00:15 > 00:00:00 + 00:00:10 -> true, finish now
// e.g.2, 00:00:08 > 00:00:00 + 00:00:10 -> false, set timer again
if (timeout != -1L && SystemClock.elapsedRealtime() > activityLaunchTime + timeout) {
this@SecondActivity.finish()
} else {
setTimer(activityLaunchTime + timeout - SystemClock.elapsedRealtime())
}
}
override fun onPause() {
super.onPause()
timer?.cancel()
}
private fun setTimer(t: Long) {
if (t == -1L) { return }
timer = object: CountDownTimer(t, 1000) {
override fun onTick(millisUntilFinished: Long) {}
override fun onFinish() {
checkTimeout()
}
}
timer?.start()
}
private fun checkTimeout() {
if (timeout == -1L) { return }
if (SystemClock.elapsedRealtime() > activityLaunchTime + timeout ) {
this@SecondActivity.finish()
}
}

}

由于安全原因,应用程序无法停止对其他应用程序有任何了解或影响。但由于您正在编写这两个应用程序,您可以在应用程序#2中创建一个导出的BroadcastReceiver,它有一个特定的意向过滤器(类似于com.my.app.SHUT_DOWN(。当它收到该意向时,让它关闭所有活动。然后在你的应用程序#1中,每当你想杀死应用程序#2 时,发送该意图

这里有一个类似的问题,但它相当古老,您可能需要为最新的API级别适当地更新代码