【问题标题】:Accessing data/app fails despite granted su permissions尽管授予了 su 权限,但访问数据/应用程序失败
【发布时间】:2012-08-16 20:11:23
【问题描述】:

目前,我正在尝试通过创建一个基于字符串数组的 ListView 来快速了解我手机上所有已安装的应用程序,该字符串数组包含数据/应用程序和系统/应用程序目录中包含的所有文件:

我的代码如下:

public void onCreate(Bundle icicle) {
super.onCreate(icicle);

Process p;
try {
   // Preform su to get root privledges
   p = Runtime.getRuntime().exec("su"); 

   // Attempt to write a file to a root-only
   DataOutputStream os = new DataOutputStream(p.getOutputStream());
   os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");

   // Close the terminal
   os.writeBytes("exit\n");
   os.flush();
   try {
      p.waitFor();
           if (p.exitValue() != 255) {
                File dir = new File("./system/app");
                File dir2 = new File("./data/app");
                String[] values = this.both(dir.list(), dir2.list());
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, values);
                setListAdapter(adapter);
           }
           else {
               Toast.makeText(this, "not root", Toast.LENGTH_LONG).show();
           }
   } catch (InterruptedException e) {
       Toast.makeText(this, "not root", Toast.LENGTH_LONG).show();
   }
} catch (IOException e) {
    Toast.makeText(this, "not root", Toast.LENGTH_LONG).show();
}

}

(取自http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/

这两种方法如下所示:

  private String[] both(String[] first, String[] second) {
    List<String> both = new ArrayList<String>(first.length + second.length);
    Collections.addAll(both, first);
    Collections.addAll(both, second);
    return both.toArray(new String[both.size()]);
  }

但是,我的应用程序不断崩溃。通过删除代码的相应部分,我能够找出原因确实是“new File(”./data/app");”部分。

【问题讨论】:

  • 您可以使用 packagemanager 轻松获取此信息,而不是从目录中列出应用程序
  • 谢谢,我会看看各自的 API。不过,我想有一个解决我的问题。

标签: java android permissions root


【解决方案1】:

通过运行

p = Runtime.getRuntime().exec("su");

您正在创建一个具有 root 访问权限的新进程。但是您的应用程序在不同的进程中运行。所以你的应用程序将没有 root 访问权限。

一种方法是在 /data/app 上使用 chmod 为您的应用授予权限,然后在 dir.list() 结束后将其恢复为原始状态。

os.writeBytes("chmod 744 \data\app \n");
os.flush();

【讨论】:

  • 谢谢,看来这确实是问题所在!你能告诉我原来的权限是什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多