0

I want to kill another app from my Application once the other app comes into foreground.

I am already capturing if my app is in foreground or background. When my app gets to background, I want to filter for the right process by it's name and use its pid to kill it. I'm using ActivityManager.getRunningAppProcesses() and filtering the processes for the wanted name, but only processes related to my app are shown. That's the first problem, because I cannot access the pid of the other apps processes.

The next problem is that, even by using the current pid (from adb) of the other apps process, my code which should kill it does not work (the other app keeps running).

I am using <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> and <uses-permission android:name="android.permission.GET_TASKS" />.

I assume that I would need be root to kill other apps, so also something like Runtime.getRuntime().exec(arrayOf<String>("su", "-c", "kill 24043")) does not work.


val amg = getSystemService(ACTIVITY_SERVICE) as ActivityManager
var processes = amg.getRunningAppProcesses();
for (info in processes) {
    if (info.processName.equals(otherAppName)) {
        val process = Runtime.getRuntime().exec(arrayOf<String>("su", "-c", "kill $pid"))

        android.os.Process.killProcess(info.pid)
        android.os.Process.sendSignal(info.pid, android.os.Process.SIGNAL_KILL)
        amg.killBackgroundProcesses(info.processName)
    }
}

The last three lines result in the signal being send, but nothing happening and the one before obviously leads to java.io.IOException: Cannot run program "su": error=13, Permission denied.

Is there another way to kill another app, that doesn't require to be root?

D. PRAKASH
  • 305
  • 1
  • 12

1 Answers1

1

You can't kill another app for security reasons. As a user, i don't want to install an app that close my apps without a good reason and without I even know about it, it would be pretty weird. That being said, it's possible to close background process from another app's, I didn't test it by myself but here is:

ActivityManager manager = (ActivityManager)getSystemService(Activity.ACTIVITY_SERVICE);
manager.killBackgroundProcesses(packageName);

You'll need to put this on your Manifest as well:

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
Shermano
  • 1,100
  • 8
  • 31