国产成人v爽在线免播放观看,日韩欧美色,久久99国产精品久久99软件,亚洲综合色网站,国产欧美日韩中文久久,色99在线,亚洲伦理一区二区

學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > android java如何執(zhí)行l(wèi)inux命令

android java如何執(zhí)行l(wèi)inux命令

時(shí)間: 春健736 分享

android java如何執(zhí)行l(wèi)inux命令

  android java如何執(zhí)行l(wèi)inux命令?android的程序基于java開(kāi)發(fā),當(dāng)我們接上調(diào)試器,執(zhí)行adb shell,就可以執(zhí)行l(wèi)inux命令,下面跟著學(xué)習(xí)啦小編來(lái)一起來(lái)了解下吧。

  android java執(zhí)行l(wèi)inux命令方法

  通常有很多命令可以通過(guò)串口終端輸入,得到結(jié)果,在程序中,通常我們需要得到某些命令的結(jié)果,在代碼中執(zhí)行命令,并將結(jié)果返回。

  有兩個(gè)類:CommandHelper.java和CommandResult.java 第一個(gè)類用于處理命令,第二個(gè)則用于返回一個(gè)結(jié)果

  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);//創(chuàng)建一個(gè)字進(jìn)程,并保存在process對(duì)象中

  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() 表示 等這條語(yǔ)句執(zhí)行完后再往下執(zhí)行

  //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;

  }

  }

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

  簡(jiǎn)單的調(diào)用: 殺掉一個(gè)進(jìn)程 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如何執(zhí)行l(wèi)inux命令相關(guān)文章:

1.android js調(diào)用java方法

2.linux下啟動(dòng)java程序的通用腳本介紹

3.Linux常用網(wǎng)絡(luò)服務(wù)端口

4.初學(xué)者需要學(xué)會(huì)Linux的命令

5.Linux怎么搭建Android開(kāi)發(fā)環(huán)境

6.如何解決Linux系統(tǒng)下的Java進(jìn)程消失

1368964