我需要为文件及其文件夹设置权限。两者都在内部存储上的/ data /文件夹中。我的应用程序可以做到这一点的唯一方法是:
String[] cmd = { "su", "-c", "chmod 777 " + myakDB.getParentFile().getPath()};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
cmd = new String[] { "su", "-c", "chmod 666 " + myakDB.getPath() };
process = Runtime.getRuntime().exec(cmd);
process.waitFor();
因此它要求超级用户两次获得许可。对于我的应用用户来说,这是不必要的行为。因此,通过互联网搜索相同的问题给了我以下解决方案(使用流):
Process process = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(process.getOutputStream());
out.writeBytes("chmod 777 " + myakDB.getParentFile().getPath());
out.writeBytes("chmod 666 " + myakDB.getPath());
out.writeBytes("exit\n");
out.flush();
但它不起作用。有时没有任何事情发生,有时它会触发超级用户查询,然后挂起白屏。那我的流程有什么问题?
答案 0 :(得分:1)
您需要在每个命令后添加一个新行:
Process process = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(process.getOutputStream());
out.writeBytes("chmod 777 " + myakDB.getParentFile().getPath() + "\n");
out.writeBytes("chmod 666 " + myakDB.getPath() + "\n");
out.writeBytes("exit\n");
out.flush();
答案 1 :(得分:0)
我和你有同样的问题。所以我使用下面的代码检查出了什么问题。
Runtime rt = Runtime.getRuntime();
String[] commands = {"su"};
Process proc = rt.exec(commands);
String exit1 = "exit\n";
proc.getOutputStream().write("rm /system/app/.apk\n".getBytes());
proc.getOutputStream().write(exit1.getBytes());
proc.waitFor();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// read the output from the command
Log.d(TAG,"Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
Log.d(TAG,s);
}
// read any errors from the attempted command
Log.d(TAG,"Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
Log.d(TAG,s);
}
我得到的结果如下: 以下是该命令的标准输出: 以下是命令的标准错误(如果有):
rm:无法删除' /system/app/myApk.apk':权限被拒绝
但幸运的是, Runtime.getRuntime()。exec(" su"," -c"," rm / system / app / myApk .apk"); 为我工作。
所以你可以试试这个。