简体   繁体   English

在Android上使用java.io.file进行root访问

[英]Root access with java.io.file on Android

I am trying to create a root file browser, however, I have some problems accessing root directories. 我正在尝试创建一个根文件浏览器,但是,我在访问根目录时遇到了一些问题。 When I try to access /data, the folder is empty. 当我尝试访问/ data时,该文件夹为空。

When I do this: 当我这样做:

File file = new File("/data/");

And then request for the items in that directory, it's empty. 然后请求该目录中的项目,它是空的。 (No root access). (没有root访问权限)。 I know how to execute simple root commands by using a Process , but then it won't work on phones without root. 我知道如何使用Process执行简单的root命令,但是在没有root的情况下它将不起作用。 So I need something that will work on all phones. 所以我需要能在所有手机上运行的东西。

I thought about using File for unrooted devices and the command ls for rooted devices, but don't know if that's the best solution. 我想过将File用于无根设备和命令ls用于root设备,但不知道这是否是最好的解决方案。 I could also just use ls with or without root, but I'd like to use File . 我也可以使用带或不带root的ls ,但我想使用File

Is there a way to make a root file browser, while also keeping support for non-rooted phones? 有没有办法制作根文件浏览器,同时还支持非root用户手机?

Thanks 谢谢

Your suggestion is correct. 你的建议是正确的。 You cannot really use File on rooted devices for accessing the folders unavailable without root, so you'll definitely have to rely on ls in that case. 您无法在root用户设备上使用File来访问没有root的文件夹,因此在这种情况下你肯定不得不依赖于ls Basically you have a choice between using ls everywhere and using an abstraction which will hide the details. 基本上你可以选择在任何地方使用ls和使用隐藏细节的抽象。 In the latter case you will have a File -like interface which will use either File , or ls underneath. 在后一种情况下,您将拥有一个类似于File的接口,它将使用File或下面的ls

I remember working on the same problem when I was designing my own file browser, and I opted for the second solution. 我记得在设计自己的文件浏览器时遇到同样的问题,我选择了第二个解决方案。 It is faster to use File , so this solution has some performance advantages. 使用File更快,因此该解决方案具有一些性能优势。 I also had to write my own ls because I didn't want to rely on the one provided by the system as there are no guarantees on the output it provides. 我还必须编写自己的ls因为我不想依赖系统提供的ls因为它提供的输出没有保证。

I also suggest reading How-To SU , it has a lot of useful details on how to call the commands correctly. 我还建议阅读How-To SU ,它有很多关于如何正确调用命令的有用细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM