Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尽管已授予su权限,但访问数据/应用程序失败_Java_Android_Permissions_Root - Fatal编程技术网

Java 尽管已授予su权限,但访问数据/应用程序失败

Java 尽管已授予su权限,但访问数据/应用程序失败,java,android,permissions,root,Java,Android,Permissions,Root,目前,我正试图通过创建一个基于字符串数组的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")

目前,我正试图通过创建一个基于字符串数组的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();
}
  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()]);
  }
public void onCreate(捆绑冰柱){
超级冰柱;
过程p;
试一试{
//预成型su以获得根部特权
p=Runtime.getRuntime().exec(“su”);
//尝试仅将文件写入根目录
DataOutputStream os=新的DataOutputStream(p.getOutputStream());
os.writeBytes(“echo\”我有根吗?\”>/system/sd/temporary.txt\n);
//关闭航站楼
os.writeBytes(“退出”);
os.flush();
试一试{
p、 waitFor();
如果(p.exitValue()!=255){
文件目录=新文件(“./system/app”);
文件dir2=新文件(“./data/app”);
String[]value=this.both(dir.list(),dir2.list());
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,值);
setListAdapter(适配器);
}
否则{
Toast.makeText(这个“不是根”,Toast.LENGTH_LONG.show();
}
}捕捉(中断异常e){
Toast.makeText(这个“不是根”,Toast.LENGTH_LONG.show();
}
}捕获(IOE异常){
Toast.makeText(这个“不是根”,Toast.LENGTH_LONG.show();
}
}

(摘自)

这两种方法如下所示:

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();
}
  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()]);
  }
private String[]两者(第一个字符串[],第二个字符串[]){
列出两者=新的ArrayList(first.length+second.length);
Collections.addAll(两个,第一个);
集合。addAll(两个,第二个);
返回both.toArray(新字符串[both.size()]);
}
然而,我的应用程序不断崩溃。通过删除代码的相应部分,我能够找到原因,确实是“新文件(“./data/app”);”部分。

通过运行

p=Runtime.getRuntime().exec(“su”)

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

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


您可以使用packagemanager轻松获取此信息,而不是从DirectoriesHanks列出应用程序。我将查看相应的API。尽管如此,我还是想找到一个解决问题的办法。谢谢,看来这确实是个问题!你能告诉我最初的权限是什么吗?