我们可以使用我们的应用程序关闭/终止另一个应用程序吗?

时间:2020-09-12 18:29:13

标签: android foreground-service device-admin background-foreground

我有一个应用,其中有一个device admin permissionforeground service

现在我想close/terminate particular app出现在前台时。我知道另一个应用程序的软件包名称。

假设我打开chrome (com.android.chrome)时。现在,当它进入前台时,我想关闭/终止该应用程序,并从最近的项目中删除。

有什么办法吗?

1 个答案:

答案 0 :(得分:0)

由于您是作为无障碍服务运行的,因此您应该能够检测到该应用中的任何事件,然后确保将其杀死。一种方法是使用活动管理器:

class AccessibilityService : AccessibilityService() {
    override fun onInterrupt() {
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if (event?.packageName == "com.whatever.app") {
            val am = applicationContext.getSystemService(ACTIVITY_SERVICE) as ActivityManager
            am.killBackgroundProcesses(event?.packageName)
        }
    }
}

或者只是使用执行:

Runtime.getRuntime().exec("am kill ${event?.packageName}")