Violet Giraffe Violet Giraffe - 1 year ago 73
Android Question

How to kill a 3rd-party application?

My program needs to kill a specific application. Is it possible on a stock, unrooted device? If yes - how? I know its process name and PID.

Answer Source

Once again I am answering too late but since I run on a same situation today I thought to share my findings in order to help someone. First of all you need to understand what you can kill and what not. By android's point of view an application is not like other OSes. An android application consists of many components (activities, broadcast receivers, services, most important tasks etc) which are packed in a package. A package can have more that one processes running depending on its components running. Now the interesting part is that an android package isn't considered (by android) "killed" or "stopped" if any or all of its processes have killed, in fact a package can still running even with no processes running at all. You can see this effect if you start an emulator start a program (i.e. Browser) and then kill its process via DDMS, after that go to the application's package settings (Settings --> Applications --> Manage Applications --> All --> Browser), you can see the "Force Stop" button enabled, this means that the application is still running (from android's point of view). What happened here is that the application has one or more tasks "frozen". That is, android has saved the state of the application's activities (task or tasks) and so the package is still running or better if the user returns to it he will land on the last thing he was doing. Now if you click the "Force Stop" button, android will discard all of these "frozen" tasks and when the user returns to the application he will see the first activity. A Task is something you cannot kill (since froyo) only the user (from "Force Stop" button), the system or a third party application which is signed with the same key of the system can do that (and maybe a root capable application but I have not confirmed this). On the other hand a Process is something you can kill and reclaim the memory it uses, as long as you follow some restrictions:

  1. You have the "android.permission.KILL_BACKGROUND_PROCESSES" permission.
  2. The processes are not system or root processes.
  3. The process is not belonging to a component which is persistent.
  4. The process is not a critical for the system to operate by any other means.

Besides the no 1 rule you do not have to do something about them, android will take care of this.

ActivityManager has a handy function you can use in order to kill all of the processes a package has at once. When you invoke it android will kill any process can be killed and thus freeing up some memory. However the state of the tasks for this package will be saved and when the user returns to the application he will see the last thing he was doing unless the system itself has killed them. This can occur either because it needs resources or the state was saved long time ago (about 30 minutes). The side-effect is that because users are thinking that all applications are like in desktop operating systems, they do not believe that the application is really closed but this is the life with android.

Now to the code:

For my project I have prepared three functions to achieve this.

The first one looks for the first process pid a package may have and it returns -1 if there aren't any.

private Context cx;
private ActivityManager am = (ActivityManager) cx.getSystemService(Context.ACTIVITY_SERVICE);

public int findPIDbyPackageName(String packagename) {
    int result = -1;

    if (am != null) {
        for (RunningAppProcessInfo pi : am.getRunningAppProcesses()){
            if (pi.processName.equalsIgnoreCase(packagename)) {
                result =;
            if (result != -1) break;
    } else {
        result = -1;

    return result;

The second does something stupid but I need it for my project.

public boolean isPackageRunning(String packagename) {
    return findPIDbyPackageName(packagename) != -1;

The third does the job.

public boolean killPackageProcesses(String packagename) {
    boolean result = false;

    if (am != null) {
        result = !isPackageRunning(packagename);
    } else {
        result = false;

    return result;

They are confirmed to work with emulator API 8 and 9 and on a real device (Galaxy S2) with API 15 and they DO kill any application's processes (not just your own) as long as they aren't needed.

Now about the android.os.Process.killProcess documentation which states that:

... Typically this means only the process running the caller's packages/application and any additional processes created by that app; ...

I believe (as you may noticed my English aren't good enough) that by "the process running the caller's packages/application" means the home launcher application and NOT your own application. Your application is the "caller" and the process running the "caller's packages/application" is the home launcher or any other app launched your application. This is the only way for me to explain that the killBackgroundProcesses function and the android.os.Process.killProcess function are indeed work on third party applications.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download