有沒有辦法在我的應用程序上運行終端命令,然後訪問我的用戶界面上的數據?具體爲top
。以編程方式在android上運行shell命令的任何方式?
回答
查看日誌收集器作爲示例。這裏是relevant file。
的關鍵是在這裏:
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("logcat");//$NON-NLS-1$
[...]
Process process = Runtime.getRuntime().exec(commandLine);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
這也取決於你在終端運行的是什麼...如果你是在你也可以做這樣的一個文件上運行「貓」。
final private String MEM_FILE = "/proc/meminfo";
public Long readMem() {
String[] segs;
FileReader fstream;
try {
fstream = new FileReader(MEM_FILE);
} catch (FileNotFoundException e) {
Log.e("readMem", "Could not read " + MEM_FILE);
return false;
}
BufferedReader in = new BufferedReader(fstream, 500);
String line;
try {
while ((line = in.readLine()) != null) {
if (line.indexOf("MemTotal:") > 0) {
Log.e("MemTotal", line);
segs = line.trim().split("[ ]+");
memTotal = Long.parseLong(segs[1]);
}
if (line.indexOf("MemFree:") > 0) {
Log.e("MemFree", line);
segs = line.trim().split("[ ]+");
memFree = Long.parseLong(segs[1]);
}
}
updateMem(); //call function to update textviews or whatever
return true;
} catch (IOException e) {
Log.e("readMem", e.toString());
}
return false;
}
編輯: 有一個名爲netmeter的Android實驗項目給你一個很好的例子。有一個名爲Top.java的類實際上完全按照您的需要進行,並將其用於TaskList.java中以供顯示。 http://code.google.com/p/android-labs/source/browse/#svn/trunk/NetMeter/src/com/google/android/netmeter
不是貓,我特別想跑到頂端,並獲得屏幕上顯示的實時結果。我知道模擬終端的應用程序,我發現它的頂級工作,所以我想知道如何才能拉出頂部的結果並在UI上顯示它們。 – Shouvik 2010-07-28 11:33:50
查看我上面的編輯,瞭解您想要的正確工作示例。 – androidworkz 2010-07-28 12:42:31
嘿謝謝...需要在這裏度過一段時間,但我認爲這是...你一直在幫助很大! – Shouvik 2010-07-29 05:05:58
好吧,這到底是什麼,以防萬一在嘗試爲我工作的人需要它在未來... :)
環繞,趕上
try {
Process process = Runtime.getRuntime().exec("top -n 1 -d 1");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
} catch (InterruptedException e) {
e.printStackTrace();
}
當我這樣做後跟String result = null; result = bufferedReader.readLine(); Log.i(「OnClickListener」,「result:」+ result);它只顯示「result:」('result'字符串爲空)。任何想法,爲什麼會這樣?另外,當我嘗試process = Runtime.getRuntime()。exec(「/ system/bin/ping abc」);相反,'結果'字符串爲空。這兩個命令都可以像'adb shell'終端一樣按預期工作。 – 2013-09-17 17:15:40
對不起@DavidDoria,我現在兩年沒碰過android。這是我在大學期間嘗試過的一些事情。你最好把它作爲一個問題發佈。 – Shouvik 2013-09-20 16:01:13
我得到它的工作。問題是,'top'的第一行是空的(一個新行,創建表)。實際輸出時它返回null的情況是因爲我需要得到stderror(getErrorStream)而不是stdout(getInputStream)。 – 2013-09-20 17:38:17
我們,可以執行命令,跟着,我成功地做到了這一點....!像這樣嘗試,這裏我們需要指定完整的命令路徑。獲得條命令的完整路徑,在烏爾終端(機器人)型
* $其中LS
/系統/箱*
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
- 1. Android |以編程方式運行命令
- 2. 以編程方式執行shell命令
- 3. 如何以編程方式等待Shell命令完成運行?
- 4. 配置android以編程方式運行ffmpeg命令
- 5. 以編程方式執行vim命令?
- 6. PseudoTTY以編程方式執行命令
- 7. 如何以編程方式在android中執行終端命令
- 8. 通過命令行以編程方式運行程序
- 9. 如何以編程方式退出基於tcl的命令shell?
- 10. 以定製命令行方式運行新的zsh和bash shell
- 11. 如何以編程方式調用Felix/Karaf shell命令?
- 12. 以編程方式運行CUDA命令行分析器
- 13. 如何打開網頁並以編程方式在其上運行JavaScript命令?
- 14. 以編程方式打開Gnome終端並運行命令
- 15. 以編程方式創建/運行命令文件
- 16. 以編程方式使用Primefaces運行命令
- 17. 以編程方式在android中的FFmpeg命令
- 18. 如何以編程方式運行testng?
- 19. 以編程方式在android
- 20. 以任何方式在.NET中以編程方式添加HttpHandler?
- 21. 以編程方式在java中執行終端中的命令
- 22. 以編程方式在Android上動畫
- 23. 重命名Android apk以編程方式
- 24. 我需要什麼權限才能在Android中以編程方式運行shell命令?
- 25. 任何方式來使Android應用程序運行在Linux上?
- 26. 以便攜方式以編程方式更改shell的cwd
- 27. 如何從node.js以編程方式執行mongodump命令?
- 28. 是否有任何非Windows方式從本地.html文件運行shell命令?
- 29. 在Eclipse控制檯中以編程方式執行命令
- 30. 以任何方式在web上下文之外以編程方式訪問Sitecore?
所以基本上,如果我想上運行,我可以只需用頂部替換commandLine,它應該正常工作嗎?我得到一個錯誤,所以我想我需要更多的幫助..謝謝.. – Shouvik 2010-07-28 11:40:57
我是否需要包圍進程process = Runtime.getRuntime()。exec(commandLine);嘗試和趕上,因爲我不斷收到這拋出IOException。我看了java的例子似乎處理它的方式... – Shouvik 2010-07-28 12:33:06
是的..再次,看看鏈接,看看他們做的一切。你肯定需要做錯誤檢查 - 不要指望'頂部'出現在所有手機上。 – EboMike 2010-07-28 16:15:07