Skip to content Skip to sidebar Skip to footer

Kill Other Process In Android

Can I end or kill other process running, programmatically? I'm asked my friend and tell me should using something like kiosk mode !! Could anyone help me to solve this problem .. A

Solution 1:

Recently I was investigating the same question, because I got tired of manually having to close the Google Maps application that regularly automatically opens and keeps running in the background and draining my battery...

But I don't think that Android lets you kill any processes but your own. This is good for security purposes, but I would at least liked them to include an application permission so programs are able to do it if allowed by the user.

I tried the following approaches and not one of them is working:

  • activityManagerObject.killBackgroundProcesses(packageName)
  • Process.killProcess(pid)
  • Process.sendSignal(pid, Process.SIGNAL_KILL)

It was pointed out by a user that it is not possible since Android 2.1. See here: https://stackoverflow.com/a/6535201/4090002

Since the Android OS has other limitations for us users as well, users are forced to live with silly limitations and reduced rights to do anything about them. I don't know about the other mobile OS, but this is where Android s***s big time.

Solution 2:

Since Android 2.2 (or higher, I don't remember), you can only kill your own app using an intent. To kill another processes from your app you must have rooted device, parse the ps command output manually, get needed process ids, exec the su, write the kill command to this pipe, and here we go. I wrote this code for my own library, so this methods are separated, but hope you'll understand:

publicinterfaceExecListener {
        voidonProcess(String line, int i);
    }

    publicstatic String exec(String input)throws IOException, InterruptedException {
        return exec (input, null);
    }

    publicstatic String exec(String input, ExecListener listener)throws IOException, InterruptedException {

        Processprocess= Runtime.getRuntime ().exec (input);
        BufferedReaderin=newBufferedReader (newInputStreamReader (process.getInputStream ()));

        StringBuilderoutput=newStringBuilder ();

        inti=0;
        String line;

        while ((line = in.readLine ()) != null) {

            if (listener == null)
               output.append (line).append ("\n");
            else
               listener.onProcess (line, i);

            ++i;

        }

        in.close ();
        process.waitFor ();

        return output.toString ().trim ();

    }

    publicstaticvoidexecSU(List<String> cmds)throws IOException, InterruptedException {

        Processprocess= Runtime.getRuntime ().exec ("su");
        DataOutputStreamos=newDataOutputStream (process.getOutputStream ());

        for (String cmd : cmds) os.writeBytes (cmd + "\n");

        os.writeBytes ("exit\n");

        os.flush ();
        os.close ();

        process.waitFor ();

    }

    publicstaticvoidkillProcess(String id)throws IOException, InterruptedException {
        return killProcess (newString[] {id});
    }

    publicstaticvoidkillProcess(final String[] ids)throws IOException, InterruptedException {

        final ArrayList<String> procs = newArrayList<> ();

        exec ("ps", newExecListener () {

            @OverridepublicvoidonProcess(String line, int i) {

                if (i > 0) {

                    String[] data = Arrays.explode ("\\s+", line);

                    for (String id : ids)
                      if (id.equals (data[8]))
                        procs.add (data[1]);

                }

            }

        });

        if (procs.size () > 0) {

            ArrayList<String> cmds = newArrayList<> ();
            cmds.add ("kill -9 " + Arrays.implode (" ", procs));

            execSU (cmds);

        }

    }

Solution 3:

Just to make it clear and this is how it worked for me for rooted phone (which means su command is available to the device):

Runtime.getRuntime().exec ("sh -c su -c kill -9 pid") 

where pid is the process id of the app you want to kill, which should be an number.

Please note this is also system dependent. In some system without "sh -c ", it would work. In some system, you need specify uid after su command:

su uid 

Hope it helps.

David

Solution 4:

I think you can try something like android.os.Process.killProcess(android.os.Process.myPid());

but if you want kill other processes youd must have root permission (in general, you haven't it)

Solution 5:

try this code

kill all process but not system process

// TODO clean servise

ActivityManagermActivityManager= (ActivityManager) this
        .getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> rs = mActivityManager
        .getRunningServices(500);

Intent destrIntent;
for (inti=0; i < rs.size(); i++) {
    ActivityManager.RunningServiceInforsi= rs.get(i);

    if (!rsi.service.getClassName().contains("com.android")
            || !rsi.service.getPackageName().contains("com.android")) {

        android.os.Process.sendSignal(rsi.pid,
                android.os.Process.SIGNAL_KILL);
        mActivityManager.killBackgroundProcesses(rsi.service
                .getPackageName());
    }
}

Post a Comment for "Kill Other Process In Android"