gpt4 book ai didi

android - 从 android Activity 执行 su 命令

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:23 25 4
gpt4 key购买 nike

我正在尝试执行以下方法:

public void runAsRoot(String[] cmds) throws Exception {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
InputStream is = p.getInputStream();
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
int readed = 0;
byte[] buff = new byte[4096];

// if cmd requires an output
// due to the blocking behaviour of read(...)
boolean cmdRequiresAnOutput = true;
if (cmdRequiresAnOutput) {
while( is.available() <= 0) {
try { Thread.sleep(200); } catch(Exception ex) {}
}

while( is.available() > 0) {
readed = is.read(buff);
if ( readed <= 0 ) break;
String seg = new String(buff,0,readed);
Log.i("#>", seg);
}
}
}
os.writeBytes("exit\n");
os.flush();
}

我使用以下输入调用了这个方法:

String[] cmds = {"/system/bin/sendevent /dev/input/event0 1 107 0 \n", "sleep 1", "/system/bin/sendevent /dev/input/event0 1 107 1 \n"};
try {
runAsRoot(cmds);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

但在 logcat 中我收到以下错误:

07-06 15:19:27.007: E/su(6547): sudb - Opening database
07-06 15:19:27.007: E/(6547): Couldn't open database: unable to open database file
07-06 15:19:27.017: E/su(6547): sudb - Could not open database, prompt user
07-06 15:19:47.082: E/su(6547): select failed with 2: No such file or directory
07-06 15:19:47.082: W/su(6547): request rejected (10060->0 /system/bin/sh)

知道问题出在哪里吗?

最佳答案

看起来 su 二进制文件有问题,而不是您的应用程序。检查您是否可以从“adb shell”成功运行 root shell。如果“adb shell”从一开始就给你一个 root shell,运行“su 1000”以失去 root 权限,然后运行“su”尝试再次进入 root shell。如果失败,则 su 不工作。

哦,还有一个相关的注意事项:一定要在另一个线程中运行 su,可能是通过 Handler 或 AsyncTask,这样它就不会阻塞您的 UI 线程。

关于android - 从 android Activity 执行 su 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11359999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com