0

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

我的代码如下:

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");” 部分。

4

1 回答 1

1

通过运行

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

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

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

os.writeBytes("chmod 744 \data\app \n");
os.flush();
于 2012-08-16T22:35:36.583 回答