Android Shell Run findProcessIdWithPidOf(String command)

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

Description

find Process Id With Pid Of

License

Open Source License

Declaration

public static int findProcessIdWithPidOf(String command)
            throws Exception 

Method Source Code

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

import java.io.BufferedReader;
import java.io.File;

import java.io.InputStreamReader;

import android.util.Log;

public class Main {
    public final static String SHELL_CMD_PIDOF = "pidof";

    public static int findProcessIdWithPidOf(String command)
            throws Exception {

        int procId = -1;

        Runtime r = Runtime.getRuntime();

        Process procPs = null;//from w ww  .  java  2  s.  c  o m

        String baseName = new File(command).getName();
        // fix contributed my mikos on 2010.12.10
        procPs = r.exec(new String[] { SHELL_CMD_PIDOF, baseName });
        // procPs = r.exec(SHELL_CMD_PIDOF);

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

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

            try {
                // this line should just be the process id
                procId = Integer.parseInt(line.trim());
                break;
            } catch (NumberFormatException e) {
                Log.e("TorServiceUtils", "unable to parse process pid: "
                        + line, e);
            }
        }

        return procId;

    }
}

Related

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