run Shell Command - Android Android OS

Android examples for Android OS:Shell Command

Description

run Shell Command

Demo Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

  private final static int PROCESS_READ_BUF = 8 * 1024;

  public static List<String> runShellCommand(String cmd) throws IOException {
    Process process = Runtime.getRuntime().exec(cmd);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()),
        PROCESS_READ_BUF);//  w ww.j av a 2  s .c  o m
    List<String> resultList = new ArrayList<String>();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
      resultList.add(line);
    }

    return resultList;
  }

  public static List<String> runShellCommand(String cmd, String keyword) throws IOException {
    Process process = Runtime.getRuntime().exec(cmd);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()),
        PROCESS_READ_BUF);
    List<String> resultList = new ArrayList<String>();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
      if (line.contains(keyword)) {
        resultList.add(line);
      }
    }

    return resultList;
  }

}

Related Tutorials