gpt4 book ai didi

java - 在我的应用程序内执行命令

转载 作者:太空宇宙 更新时间:2023-11-04 10:26:45 24 4
gpt4 key购买 nike

我正在尝试从我的 Android 应用程序执行命令,以便通过代码配置我的设备的某些方面。例如,我尝试使用以下代码锁定我的设备(来自扩展 MainActivity 类的类):

Process p=Runtime.getRuntime().exec("su");
DataOutputStream dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes("adb shell input keyevent 26");
dos.writeBytes("exit\n");
dos.flush();
dos.close();
p.waitFor();

当我运行它时,我收到:

java.io.Exception: Error running exec(). Command: [su] Working directory: null Environment: null

我的设备未取得 root 权限。我尝试过其他方法来做到这一点,但没有成功。

有什么想法吗?

最佳答案

我认为您无法在不root应用程序的情况下执行此操作。因此,请尝试在已取得 root 权限的设备上执行此操作。

获得 root 权限后,您可以尝试检查 root 权限,然后执行这些任务。

如前所述检查 root 权限 here :

 public boolean isRooted() {
// get from build info
String buildTags = android.os.Build.TAGS;
if (buildTags != null && buildTags.contains("test-keys")) {
return true;
}
// check if /system/app/Superuser.apk is present
try {
File file = new File("/system/app/Superuser.apk");
if (file.exists()) {
return true;
}
} catch (Exception exception) {
// ignore
exception.printStackTrace();
}
String[] commands = {
"/system/xbin/which su",
"/system/bin/which su",
"which su"
};
for (String command : commands) {
try {
Runtime.getRuntime().exec(command);
return true;
} catch (Exception exception) {
exception.printStackTrace();
}
}
Log.d("message-startUp: ", "RootUtil");
return false;
}

关于java - 在我的应用程序内执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50453289/

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