Get the PID of a process to kill it, without knowing its full name Get the PID of a process to kill it, without knowing its full name android android

Get the PID of a process to kill it, without knowing its full name


You can use:

ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> services = manager.getRunningAppProcesses(); String service1name = services[1].processName;

You can get all running process's package names, check which one you want to kill, choose that process get process id by service.pid.

And call:

android.os.Process.killProcess(service.pid);


your process name is '/data/data/com.something.something/mybinary'first get process id of the native process running by parsing output of top and then use android.os.Process.killProcess(Pid)

             import org.apache.commons.exec.*;             import java.io.IOException;        public class NativeKillerRunnable implements Runnable {             private static final Logger logger = LoggerFactory.getLogger(NativeKillerRunnable.class);          @Override                public void run() {        String commandtoexec = "top -n 1 -m 100";        CommandLine cmdLine = CommandLine.parse(commandtoexec);       DefaultExecutor executor = new DefaultExecutor();        try {           PumpStreamHandler psh = new PumpStreamHandler(new LogOutputStream() {            @Override            protected void processLine(String s, int i) {                s = s.trim();      //check for name of your binary process                 if(s.contains("mybinary"))                {                    String[] strings = s.split(" ");                    android.os.Process.killProcess(Integer.parseInt(strings[0]));                    logger.info("killed mybinary process with pid = "+strings[0]);                }            }        });        executor.setStreamHandler(psh);        executor.execute(cmdLine);    } catch (ExecuteException executeException) {        logger.error("caught exception while killing mybinary process "+executeException.getMessage());    } }

}


I got same problem.Finally,I find a method to kill my binary process with android java code.first,write process info to a txt file:

echo `ps | grep /data/data/com.something.something/mybinary` > /sdcard/pid.txt

and then,use java code to read this file,you could get String like:

root 17857 16409 87568 14880 ffffffff aaf0ec68 R /data/data/com.something.something/mybinary

you have got the pid of your process, execute kill command in java.