2

How to kill an app in android?

killPackageProcesses(services.get(i).baseActivity.getPackageName());

this is the function killPackageProcesses

public void killPackageProcesses(String packagename) {
    int pid = 0;
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pids = am
            .getRunningAppProcesses();
    for (int i = 0; i < pids.size(); i++) {
        ActivityManager.RunningAppProcessInfo info = pids.get(i);
        if (info.processName.equalsIgnoreCase(packagename)) {
            pid = info.pid;
        }
    }
    android.os.Process.killProcess(pid);

}

It doesn't work. How does it work for other apps? The permission is written.

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
Hi Mans
  • 65
  • 2
  • 9
  • When you say it "doesn't work", what do you mean? Is there an error? Is the result something other than expected? Explain. You also need more context for your code. What's `killPackageProcesses()`? `services`? `i`? – Michelle Aug 12 '13 at 16:55
  • @Michelle Sorry. I've just edited the post. – Hi Mans Aug 12 '13 at 16:57

1 Answers1

3

You can't kill just any process in Android. From the documentation on Process:

Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.

Also note that the permission you're requesting in your manifest doesn't work the way you think it does. According to the documentation, KILL_BACKGROUND_PROCESS:

Allows an application to call killBackgroundProcesses(String).

Michelle
  • 2,830
  • 26
  • 33
  • Android is sending an signal for kill an process after the method above. But the app won't be shutdown imediatelly, am I right? – Hi Mans Aug 12 '13 at 17:01
  • 1
    On calling `killProcess()`, your app will request that the process be killed, but the kernel won't do it unless the process is part of your own application. Killing other people's apps on Android just isn't something you're supposed to do. – Michelle Aug 12 '13 at 17:05
  • I'm writing a simple task manager, how does the "killing-process" works for the other apps? As I understood, I can only request to kill a process, and Android can allow/reject it? – Hi Mans Aug 12 '13 at 17:08
  • The answer to "how to write a task manager" is "don't". Android handles its memory and the application life-cycle very well, and there are obvious security reasons as to why the system shouldn't let you arbitrarily kill other applications - this could easily go from task manager to malware. – Michelle Aug 12 '13 at 17:16
  • I have read a lot of topics about trying to "STOP" another 3rd party application within you own application. Its look like something impossible (unless you are rooted). In the other hand, all android versions let users stop any services or application manually: Settings>App>running app> STOP. So HOW COME THERE IS NO WHAY TO DO IT PROGRAMMATICALLY? Is Android a really open source program? – nnyerges Jul 22 '15 at 18:42
  • @nnyerges "Open source" doesn't mean "lets you do anything you want", it just means the code is available for you to obtain and modify. The system lets the *user* kill processes through a system interface because it's sure that the user is the one making the decision to do so. Some arbitrary app shouldn't be allowed to kill another because it, an untrusted program, might do so *without* the user's prompting or consent, e.g., malware that could just kill every other process immediately to prevent the device from functioning. – Michelle Jul 22 '15 at 19:32
  • @Michelle I understand now why it is not allowed. Very clear explanation. Thanks! – Yosidroid Feb 13 '23 at 20:20