從android執(zhí)行shell命令我正在嘗試從應(yīng)用程序模擬器終端執(zhí)行此命令(您可以在谷歌播放中找到它)在這個(gè)應(yīng)用程序中我寫su并按enter,所以寫道:screenrecord --time-limit 10 /sdcard/MyVideo.mp4然后再次按下enter并使用android kitkat的新功能開(kāi)始錄制屏幕。所以,我嘗試使用以下方法從java執(zhí)行相同的代碼:Process su = Runtime.getRuntime().exec("su");Process execute = Runtime.getRuntime().exec("screenrecord --time-limit 10 /sdcard/MyVideo.mp4");但是不起作用,因?yàn)闆](méi)有創(chuàng)建文件。顯然我在安裝了android kitkat的root設(shè)備上運(yùn)行。問(wèn)題出在哪兒?我怎么解決?因?yàn)閺慕K端模擬器工作和Java不?
3 回答

喵喵時(shí)光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
您應(yīng)該獲取su
剛剛啟動(dòng)的進(jìn)程的標(biāo)準(zhǔn)輸入并在那里寫下命令,否則您將使用當(dāng)前命令運(yùn)行命令UID
。
嘗試這樣的事情:
try{ Process su = Runtime.getRuntime().exec("su"); DataOutputStream outputStream = new DataOutputStream(su.getOutputStream()); outputStream.writeBytes("screenrecord --time-limit 10 /sdcard/MyVideo.mp4\n"); outputStream.flush(); outputStream.writeBytes("exit\n"); outputStream.flush(); su.waitFor();}catch(IOException e){ throw new Exception(e);}catch(InterruptedException e){ throw new Exception(e);}

30秒到達(dá)戰(zhàn)場(chǎng)
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
Process p;StringBuffer output = new StringBuffer();try { p = Runtime.getRuntime().exec(params[0]); BufferedReader reader = new BufferedReader( new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine()) != null) { output.append(line + "\n"); p.waitFor(); }} catch (IOException e) { e.printStackTrace();} catch (InterruptedException e) { e.printStackTrace();}String response = output.toString();return response;
添加回答
舉報(bào)
0/150
提交
取消