目前,我正在尝试通过创建基于字符串数组的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()]);
}
但是,我的应用程序一直在崩溃。通过删除代码的相应部分,我能够找出原因确实是“新文件(”./ data / app“);”一部分。
答案 0 :(得分:1)
运行
p = Runtime.getRuntime()。exec(“su”);
您正在创建一个具有root访问权限的新流程。但是您的应用程序在不同的进程中运行。因此,您的应用程序将无权访问。
一种方法是在/ data / app上使用chmod来授予你的应用程序权限,然后在dir.list()结束后将其恢复为原始版本。
os.writeBytes("chmod 744 \data\app \n");
os.flush();