導航:首頁 > 程序命令 > 安卓mkshell命令

安卓mkshell命令

發布時間:2025-05-24 03:13:13

1. android apk 怎麼執行adb shell命令

Android中執行adb shell命令的方式如下:

/**
* 執行一個shell命令,並返回字元串值
*
* @param cmd
* 命令名稱&參數組成的數組(例如:{"/system/bin/cat", "/proc/version"})
* @param workdirectory
* 命令執行路徑(例如:"system/bin/")
* @return 執行結果組成的字元串
* @throws IOException
*/
public static synchronized String run(String[] cmd, String workdirectory)
throws IOException {
StringBuffer result = new StringBuffer();
try {
// 創建操作系統進程(也可以由Runtime.exec()啟動)
// Runtime runtime = Runtime.getRuntime();
// Process proc = runtime.exec(cmd);
// InputStream inputstream = proc.getInputStream();
ProcessBuilder builder = new ProcessBuilder(cmd);
InputStream in = null;
// 設置一個路徑(絕對路徑了就不一定需要)
if (workdirectory != null) {
// 設置工作目錄(同上)
builder.directory(new File(workdirectory));
// 合並標准錯誤和標准輸出
builder.redirectErrorStream(true);
// 啟動一個新進程
Process process = builder.start();
// 讀取進程標准輸出流
in = process.getInputStream();
byte[] re = new byte[1024];
while (in.read(re) != -1) {
result = result.append(new String(re));
}
}
// 關閉輸入流
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result.toString();
}

android系統底層採用的是linux,所以adb這樣的linux指令是可以在java代碼中調用的,可以使用ProcessBuilder 這個方法來執行對應的指令。還可以通過如下方式執行:

Process p = Runtime.getRuntime().exec("ls");
String data = null;
BufferedReader ie = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String error = null;
while ((error = ie.readLine()) != null
&& !error.equals("null")) {
data += error + "\n";
}
String line = null;
while ((line = in.readLine()) != null
&& !line.equals("null")) {
data += line + "\n";
}

Log.v("ls", data);

2. 如何用shell命令打開某個手機app

android中執行shell命令有兩種方式:1.直接在代碼中用java提供的Runtime這個類來執行命令,以下為完整示例代碼。publ

閱讀全文

與安卓mkshell命令相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:618
php5314 瀏覽:359
完美國際安裝到哪個文件夾 瀏覽:671
什麼app可以掃一掃做題 瀏覽:541
程序員編碼論壇 瀏覽:926
淘點是什麼app 瀏覽:662
中國高等植物pdf 瀏覽:455
51單片機時間 瀏覽:185
後台如何獲取伺服器ip 瀏覽:269
單片機流水燈程序c語言 瀏覽:237
程序員第二職業掙錢 瀏覽:242
運行里怎麼輸入伺服器路徑 瀏覽:844
pythonstepwise 瀏覽:513
劉一男詞彙速記指南pdf 瀏覽:67
php認證級別 瀏覽:372
方舟編譯啥時候推送 瀏覽:1013
php手機驗證碼生成 瀏覽:678
哲學思維pdf 瀏覽:19
凌達壓縮機有限公司招聘 瀏覽:537
weblogic命令部署 瀏覽:40