運行exec()時出錯。命令:Runtime.exec()引發工作目錄:null環境:null
set `ps | grep <package name>`
工作目錄:空環境:空
我運行下面的命令來獲得進程ID爲我的應用程序
private int getProcessId(String packageName) {
int processid = -1;
try {
runADBCommand(new String[] {"set `ps | grep " + packageName + "`"});
processid = Integer.parseInt(runADBCommand(new String[]{"print $2"}));
} catch (IOException e) {
e.printStackTrace();
Log.e(LOG_TAG, e.getMessage());
}
Log.w(LOG_TAG, "Process id: " + processid);
return processid;
}
的runADBCommand功能如下:
private String runADBCommand(String[] adbCommand) throws IOException {
String returnValue = "", line;
InputStream inStream = null;
try {
Process process = Runtime.getRuntime().exec(adbCommand);
inStream = process.getInputStream();
BufferedReader brCleanUp = new BufferedReader(new InputStreamReader(inStream));
while ((line = brCleanUp.readLine()) != null) {
returnValue = returnValue + line + "\n";
}
brCleanUp.close();
try {
process.waitFor();
} catch (InterruptedException e) {
Log.e(LOG_TAG, e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Log.e(LOG_TAG, e.getMessage());
}
return returnValue;
}
我試圖在非根植的Moto G設備上運行此操作,並且還切斷了,aw k不是 在設備外殼中可供我直接獲取pid。
這不是一個例外,所以不會拋出。提供整個堆棧跟蹤。 – EJP