安卓(Android) 怎么运行shell命令

2019年10月13日15:22:11安卓(Android) 怎么运行shell命令已关闭评论 334 1051字阅读3分30秒

1.简介

安卓(Android) 怎么运行shell命令?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
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);
    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;
  }

}

 
继续阅读
编程十万个怎么办