Java 如何通过su执行shell命令?
我尝试了以下方法,但对我无效:Java 如何通过su执行shell命令?,java,android,android-shell,Java,Android,Android Shell,我尝试了以下方法,但对我无效: public void RunAsRoot(String cmnd){ Process p = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(p.getOutputStream()); os.writeBytes(cmnd +"\n");
public void RunAsRoot(String cmnd){
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes(cmnd +"\n");
os.writeBytes("exit\n");
os.flush();
p.waitFor();
}
我也试过这个:
public void RunAsRoot(String cmds){
Process p = Runtime.getRuntime().exec("su -c " + cmds);
p.waitFor();
}
但上述方法都不起作用。但是,以下命令通过adb在shell中对我起作用:
su -c "input" " keyevent" " 3"
所以我甚至试过这个:
public void RunAsRoot(String cmds){
Process p = Runtime.getRuntime().exec("su -c \"input\" \" keyevent\" \" 3\"");
p.waitFor();
}
但即使这样也不起作用。谁能帮帮我 我使用OutputStream而不是DataOutputStream
是否要在管理模式下运行java应用程序?设备是否已根目录?
sh = Runtime.getRuntime().exec("su", null,null);
String now = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH) + "-" + c.get(Calendar.HOUR)+":"+c.get(Calendar.MINUTE);
Log.d(TAG, "Creating screenshot " + now +".png");
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/data/local/" + now +".png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();