歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 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;

  }
 

Copyright © Linux教程網 All Rights Reserved