Java如何操作Android的adb shell 之 我自己在程序中的使用方法

package com.symbio.ltp.adb;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;


import com.symbio.ltp.model.ConfigPropertiesData;
import com.symbio.ltp.util.Log;


public class ShellCommand {
private String name;
private Process process;
private BufferedWriter writer;
private BufferedReader reader;
private BufferedReader errorReader;
private List<String> list;
private String[] returnValue;


public ShellCommand(String name) {
this.name = name;
}

public String getName() {
return name;
}

public Process getProcess() {
return process;
}

public BufferedWriter getOutputWriter() {
return writer;
}

public BufferedReader getInputReader() {
return reader;
}

public BufferedReader getErrorReader() {
return errorReader;
}

public boolean start(String cmd) {
try {
process = Runtime.getRuntime().exec(cmd);
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
} catch (IOException e) {
Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());
return false;
}
return true;
}

public boolean exec(String cmd) {
String line;

try {
writer.write(cmd + "\n");
writer.flush();

while((line = reader.readLine()) != null) {
Log.debug(line);
if(line.equals(ConfigPropertiesData.ltp_success)) {
return true;
} else if(line.equals(ConfigPropertiesData.ltp_fail)) {
return false;
}
}
} catch (IOException e) {
Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());
return false;
}
return true;
}



public String [] execReturn(String cmd) {
String line;
list = new ArrayList<String>();
try {
writer.write(cmd + "\n");
writer.flush();
line = reader.readLine();
while((line = reader.readLine()) != null) {
if(line.length()>0 && !(line.startsWith("#"))){
Log.debug(line);
list.add(line);
if(line.equals(ConfigPropertiesData.ltp_success)) {
break;
} else if(line.equals(ConfigPropertiesData.ltp_fail)) {
break;
}
}
}

int size = list.size();
returnValue = new String[size];
for (int i = 0; i < size; i++) {
returnValue[i] = list.get(i);
}
} catch (IOException e) {
Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());
return null;
}
return returnValue;
}


public void terminate() {
try {
writer.write(0x03);
writer.flush();
} catch (IOException e) {
Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());
}
}
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值