[英]Android Java not deleting files even with Root Access
I've been making an Android app that clears the Cache & Data of my application.我一直在制作一个清除应用程序缓存和数据的 Android 应用程序。 Don't forget, my device is rooted.
别忘了,我的设备已经root了。 So why doesn't this piece of code work?
那么为什么这段代码不起作用呢? It clearly has Root permissions and full paths.
它显然具有 Root 权限和完整路径。 What's the problem behind this?
这背后的问题是什么?
try {
Process p = Runtime.getRuntime().exec("su rm -r /data/user/0/com.rick.app");
p.waitFor();
p = Runtime.getRuntime().exec("su rm -r /data/user_de/0/com.rick.app");
p.waitFor();
p = Runtime.getRuntime().exec("su rm -r /storage/emulated/0/Android/data/com.rick.app");
p.waitFor();
p = Runtime.getRuntime().exec("su rm -r /data/data/com.rick.app");
p.waitFor();
p = Runtime.getRuntime().exec("su rm -r /data/app/com.rick.app");
p.waitFor();
p = Runtime.getRuntime().exec("su rm -r /storage/emulated/obb/com.rick.app");
p.waitFor();
p = Runtime.getRuntime().exec("su rm -r /storage/emulated/0/Android/data/com.rick.app/");
p.waitFor();
//p = Runtime.getRuntime().exec("reboot");
//p.waitFor();
//Runtime.getRuntime().exec("reboot");
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
Generally if the command will fail you wont notice it because you dont check exit code and dont redirect the process output.通常,如果命令失败,您不会注意到它,因为您不检查退出代码并且不重定向进程 output。 Consider this code using the ProcessBuilder
使用ProcessBuilder考虑此代码
Process process = new ProcessBuilder().inheritIO().command("sudo rm ...").start();
process.waitFor();
int exitCode = process.exitValue();
if (exitCode != 0) {
System.err.println("Delete failed with exit code " + exitCode);
}
This way we can figure out what exactly happens here.这样我们就可以弄清楚这里到底发生了什么。 As other pointed out java has also apis to delete files via File#delete or using nio Here is a snipped to delete it recursivly
正如其他人指出的那样 java 也有通过File#delete或使用 nio 删除文件的 api 这是一个递归删除它的片段
Files.walk(Paths.get("/data/data/"))
.map(Path::toFile)
.sorted(Comparator.reverseOrder())
.forEach(File::delete);
In Kotlin you can even use File#deleteRecursively在 Kotlin 你甚至可以使用 File#deleteRecursively
I just wanted to mention that you can actually get the cache directory via context.cacheDir()我只是想提一下,您实际上可以通过 context.cacheDir() 获取缓存目录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.