導航:首頁 > 程序命令 > 安卓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命令相關的資料

熱點內容
android仿ios時間選擇器 瀏覽:381
見識pdf 瀏覽:83
男孩子慰菊手冊pdf 瀏覽:532
注冊表啟動項命令 瀏覽:109
89c51單片機定時器 瀏覽:687
一般不適合做程序員的適合做啥 瀏覽:923
點在多邊形內演算法 瀏覽:494
程序員下班急忙回家 瀏覽:359
安慶php全套源碼交友類型網站源碼 瀏覽:92
浪潮伺服器公司地址 瀏覽:733
密約聊天交友app怎麼賺錢 瀏覽:280
滴滴java 瀏覽:109
phpexpires 瀏覽:218
matlab編程用什麼語言 瀏覽:70
php查找字元是否存在 瀏覽:701
可編程函數發生器 瀏覽:881
單片機正極接地視頻 瀏覽:67
利用python爬火車票 瀏覽:375
androidaidl如何工作 瀏覽:856
第三胸椎壓縮骨折什麼症狀 瀏覽:507