android java如何執(zhí)行l(wèi)inux命令
android java如何執(zhí)行l(wèi)inux命令
android java如何執(zhí)行l(wèi)inux命令?android的程序基于java開發(fā),當(dāng)我們接上調(diào)試器,執(zhí)行adb shell,就可以執(zhí)行l(wèi)inux命令,下面跟著學(xué)習(xí)啦小編來一起來了解下吧。
android java執(zhí)行l(wèi)inux命令方法
通常有很多命令可以通過串口終端輸入,得到結(jié)果,在程序中,通常我們需要得到某些命令的結(jié)果,在代碼中執(zhí)行命令,并將結(jié)果返回。
有兩個類:CommandHelper.java和CommandResult.java 第一個類用于處理命令,第二個則用于返回一個結(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)建一個字進(jìn)程,并保存在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() 表示 等這條語句執(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;
}
}
------------------------------------------------------------
簡單的調(diào)用: 殺掉一個進(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)文章:
3.Linux常用網(wǎng)絡(luò)服務(wù)端口