Android Shell Run findProcessIdWithPS(String command)

Here you can find the source of findProcessIdWithPS(String command)

Description

find Process Id With PS

License

Open Source License

Declaration

public static int findProcessIdWithPS(String command) throws Exception 

Method Source Code

//package com.java2s;
/* See LICENSE for licensing information */

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.StringTokenizer;

public class Main {
    public final static String SHELL_CMD_PS = "ps";

    public static int findProcessIdWithPS(String command) throws Exception {

        int procId = -1;

        Runtime r = Runtime.getRuntime();

        Process procPs = null;//from  w w w. j av  a  2 s  .co m

        procPs = r.exec(SHELL_CMD_PS);

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                procPs.getInputStream()));
        String line = null;

        while ((line = reader.readLine()) != null) {
            if (line.indexOf(' ' + command) != -1) {

                StringTokenizer st = new StringTokenizer(line, " ");
                st.nextToken(); // proc owner

                procId = Integer.parseInt(st.nextToken().trim());

                break;
            }
        }

        return procId;

    }
}

Related

  1. runAsRoot(String[] commands)
  2. runSimpleCommand(String command, String directory)
  3. doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor)
  4. findProcessId(String command)
  5. findProcessIdWithPidOf(String command)
  6. execRootCmd(String cmd)
  7. execRootCmdSilent(String cmd)
  8. runSuCommand(Context context, String command)
  9. runSuCommandAsync(Context context, String command)