/**
* 执行Android命令
*
* @param cmd 命令
*/
public static void execSuCmd(String cmd) {
Process process = null;
DataOutputStream os = null;
DataInputStream is = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
int aa = process.waitFor();
is = new DataInputStream(process.getInputStream());
byte[] buffer = new byte[is.available()];
is.read(buffer);
String out = new String(buffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
if (process != null) {
process.destroy();
}
} catch (Exception e) {
}
}
}
比如:
util.execSuCmd("echo 0 > /sys/class/gpio/gpio44/value");
本文介绍了一种在Android环境中使用Java代码执行root权限命令的方法。通过Runtime.getRuntime().exec()函数,结合DataOutputStream和DataInputStream,可以实现向系统发送命令并获取返回结果的功能。示例代码展示了如何利用此方法来操作GPIO接口。
2756

被折叠的 条评论
为什么被折叠?



