1

I have an app in which i have a device admin permission and foreground service.

Now i want to close/terminate particular app when that comes on foreground. I know the package name of that another app.

Let say when i open chrome (com.android.chrome). Now when it comes in foreground i want to close/terminate that app and also remove from the recent item.

Is there any way to do this ?

  • You might want to check these links: https://stackoverflow.com/questions/12036895/kill-another-application-on-android https://stackoverflow.com/questions/19604097/killbackgroundprocesses-no-working – Faisal Sep 18 '20 at 03:19
  • Killing the app should be relatively simple, knowing when the app opens is a little trickier. Can your app by an accessibility service? – MrK Sep 18 '20 at 03:40
  • Yes my app uses accessibility service. –  Sep 18 '20 at 04:12

1 Answers1

0

Since you're running as an accessibility service you should be able to detect any event from the app and then ensure it is killed. One way is to use the activity manager:

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)
        }
    }
}

Or maybe just using an execution:

Runtime.getRuntime().exec("am kill ${event?.packageName}")
MrK
  • 652
  • 1
  • 5
  • 17