android su中的字符串,android – 等到su中的命令完成
我在Android中運行一個su進程,每當用戶搖動手機時,它自己運行screencap實用程序(/ system / bin / screencap).
我想等待每個screencap完成之后我允許用戶通過搖動手機來取另一個screencap.但是,使用process.waitFor()對我來說不起作用,因為我不想關閉su進程并為每個screencap重新打開它(因為它會提示SuperUser應用程序的toast,這會干擾screencaps)
到目前為止,我有:
在服務的onCreate():
p = Runtime.getRuntime().exec("su");
os = p.getOutputStream();
在shake listener處理程序中:
if (isReady) {
isReady = false;
String cmd = "/system/bin/screencap -p " + nextScreenshotFullPath + "\n";
os.write(cmd.getBytes("ASCII"));
os.flush();
[INSERT MAGIC HERE]
isReady = true;
Bitmap bm = BitmapFactory.decodeFile(nextScreenshotFullPath);
// Do something with bm
}
這里[INSERT MAGIC HERE]是我正在尋找的東西 – 等待screencap完成的代碼片段.
解決方法:
我找到了一個方法!我使用shell命令echo -n 0(-n來防止換行)然后將其讀回來回顯單個字符(例如0).在screencap命令完成之后,shell將不會打印字符,并且InputStream#read()方法將阻塞,直到它可以讀取該字符…或者在代碼中說:
在服務的onCreate():
p = Runtime.getRuntime().exec("su");
os = p.getOutputStream();
is = p.getInputStream(); // ADDED THIS LINE //
在shake listener處理程序中:
if (isReady) {
isReady = false;
String cmd = "/system/bin/screencap -p " + nextScreenshotFullPath + "\n";
os.write(cmd.getBytes("ASCII"));
os.flush();
// ADDED LINES BELOW //
cmd = "echo -n 0\n";
os.write(cmd.getBytes("ASCII"));
os.flush();
is.read();
// ADDED LINES ABOVE //
isReady = true;
Bitmap bm = BitmapFactory.decodeFile(nextScreenshotFullPath);
// Do something with bm
}
標簽:android
來源: https://codeday.me/bug/20190703/1370269.html
總結
以上是生活随笔為你收集整理的android su中的字符串,android – 等到su中的命令完成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android json转字符串数组,转
- 下一篇: android palette组件用法,