学习啦>学习电脑>操作系统>Linux教程>

android java如何执行linux命令

春健分享

  android java如何执行linux命令?android的程序基于java开发,当我们接上调试器,执行adb shell,就可以执行linux命令,下面跟着学习啦小编来一起来了解下吧。

  android java执行linux命令方法

  通常有很多命令可以通过串口终端输入,得到结果,在程序中,通常我们需要得到某些命令的结果,在代码中执行命令,并将结果返回。

  有两个类:CommandHelper.java和CommandResult.java 第一个类用于处理命令,第二个则用于返回一个结果

  package com.example.task_aidl;

  import java.io.BufferedReader;

  import java.io.IOException;

  import java.io.InputStreamReader;

  public class CommandHelper {

  //default time out, in millseconds

  public static int DEFAULT_TIMEOUT;

  public static final int DEFAULT_INTERVAL = 1000;

  public static long START;

  public static CommandResult exec(String command) throws IOException, InterruptedException {

  Process process = Runtime.getRuntime().exec(command);//创建一个字进程,并保存在process对象中

  CommandResult commandResult = wait(process);

  if (process != null) {

  process.destroy();

  }

  return commandResult;

  }

  private static boolean isOverTime() {

  return System.currentTimeMillis() - START >= DEFAULT_TIMEOUT;

  }

  private static CommandResult wait(Process process) throws InterruptedException, IOException {

  BufferedReader errorStreamReader = null;

  BufferedReader inputStreamReader = null;

  try {

  errorStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

  inputStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

  //timeout control

  START = System.currentTimeMillis();

  boolean isFinished = false;

  for (;;) {

  if (isOverTime()) {

  CommandResult result = new CommandResult();

  result.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);

  result.setOutput("Command process timeout");

  return result;

  }

  if (isFinished) {

  CommandResult result = new CommandResult();

  result.setExitValue(process.waitFor()); //process.waitFor() 表示 等这条语句执行完后再往下执行

  //parse error info

  if (errorStreamReader.ready()) {

  StringBuilder buffer = new StringBuilder();

  String line;

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

  buffer.append(line);

  }

  result.setError(buffer.toString());

  }

  //parse info

  if (inputStreamReader.ready()) {

  StringBuilder buffer = new StringBuilder();

  String line;

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

  buffer.append(line);

  }

  result.setOutput(buffer.toString());

  }

  return result;

  }

  try {

  isFinished = true;

  process.exitValue();

  } catch (IllegalThreadStateException e) {

  // process hasn't finished yet

  isFinished = false;

  Thread.sleep(DEFAULT_INTERVAL);

  }

  }

  } finally {

  if (errorStreamReader != null) {

  try {

  errorStreamReader.close();

  } catch (IOException e) {

  }

  }

  if (inputStreamReader != null) {

  try {

  inputStreamReader.close();

  } catch (IOException e) {

  }

  }

  }

  }

  }

  ------------------------------------------------------------------------------

  package com.example.task_aidl;

  public class CommandResult {

  public static final int EXIT_VALUE_TIMEOUT=-1;

  private String output;

  void setOutput(String error) {

  output=error;

  }

  String getOutput(){

  return output;

  }

  int exitValue;

  void setExitValue(int value) {

  exitValue=value;

  }

  int getExitValue(){

  return exitValue;

  }

  private String error;

  public String getError() {

  return error;

  }

  public void setError(String error) {

  this.error = error;

  }

  }

  ------------------------------------------------------------

  简单的调用: 杀掉一个进程 kill -9 PID

  @Override

  public String killProc(String PID) throws RemoteException {

  // TODO Auto-generated method stub

  String cmd = "kill -9 "+PID;

  String reply = "";

  Log.v("cmd",cmd);

  try {

  CommandHelper.DEFAULT_TIMEOUT = 5000;

  CommandResult result = CommandHelper.exec(cmd);

  if (result != null) {

  if(result.getError()!=null)

  {

  Log.v("Output","Error:" + result.getError());

  reply = result.getError();

  }

  if(result.getOutput()!=null)

  {

  Log.v("Output","Output:" + result.getOutput());

  reply = result.getOutput();

  }

  }

  } catch (IOException ex) {

  Log.v("Output","IOException:" + ex.getLocalizedMessage());

  } catch (InterruptedException ex) {

  Log.v("Output","InterruptedException:" + ex.getLocalizedMessage());

  }

  return reply;

  }

android java如何执行linux命令相关文章:

1.android js调用java方法

2.linux下启动java程序的通用脚本介绍

3.Linux常用网络服务端口

4.初学者需要学会Linux的命令

5.Linux怎么搭建Android开发环境

6.如何解决Linux系统下的Java进程消失

    1368964