2012-09-29 98 views
0

我正在开发一个可以与rooted设备一起工作的应用程序。根su命令

我有两个问题:

  1. 当我启动应用程序,它会检查根,将出现超级用户对话框,我点击接受,然后“记住我的选择”后,我运行一个命令:

    Process process; 
    try { 
        process = Runtime.getRuntime().exec(new String[] 
          {"su", "-c", "rm -r /data/data"}); 
        prefs = this.getSharedPreferences("Prefs", 
          Context.MODE_WORLD_WRITEABLE); 
        prefsEditor = prefs.edit(); 
        stopSelf(); 
    

    然后再次出现超级用户对话框。为什么同一个应用程序出现多次?我检查了“记住我的选择”。

  2. 我使用

    process = Runtime.getRuntime().exec(new String[] 
          {"su", "-c", "rm -r /data/data"}); 
    

有没有办法添加例外,例如Do not delete "com.My.App"

+1

答案时是有帮助的我的问题,然后我接受了,但我会是“少”难以接受的答案现在... – CELB

回答

1

您正在删除/ data/data及其所有子目录。这是应用程序存储应用程序私有数据的地方,SuperUser肯定会在这里存储授权应用程序的列表。

我相信你已经猜到了什么是异想天开......你正在删除自己的授权。

您需要向superUser添加一个异常。

要添加一个异常,我找不到一个简单的解决方案,因为只有有限的shell命令可用。如果你安装了busybox,它会给你机会使用grep命令来解析输入并排除你想要的行。

另外,程序可以用下面的方法做:

process = Runtime.getRuntime().exec(new String[] {"su", "-c", "ls /data/data"}); 

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 

String line; 
ArrayList<String> files = new ArrayList<String>(); 
files.add("su"); 
files.add("-c"); 
files.add("rm -r"); 

while ((line = bufferedReader.readLine()) != null){ 
    //test if you want to exclude the file before you add it 
    files.add("/data/data/" + line); 
} 
//issue a new command to remove the directories 
process = Runtime.getRuntime().exec(files.toArray(new String[0])); //changed this line 

希望它帮助。

--EDITED--

代码波纹管有根设备上工作的罚款。最后发出的命令也是ls,因为我不想删除我的文件,但您可以用其他任何东西替换它(请参阅文件中的注释)。

private void execCmd(){ 
    Process process; 
    try { 
     process = Runtime.getRuntime().exec(new String[] {"su", "-c", "ls /data/data"}); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return; 
    } 

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 

    String line; 
    ArrayList<String> files = new ArrayList<String>(); 
    files.add("su"); 
    files.add("-c"); 
//  files.add("rm -r"); //Uncomment this line and comment the line bellow for real delete 
    files.add("ls"); 

    try { 
     while ((line = bufferedReader.readLine()) != null){ 
      //test if you want to exclude the file before you add it 
      files.add("/data/data/" + line); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    //comment lines bellow to stop logging the command being sent 
    Log.d(TAG, "Command size: " + files.size()); 
    for(int i=0; i< files.size(); i++) 
     Log.d(TAG, "Cmd[" + i + "]: " + files.get(i)); 

    try { 
     process = Runtime.getRuntime().exec(files.toArray(new String[0])); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } //changed this line 
} 

问候

+0

感谢您对你非常有用和回答,但是我在这一行有一个错误:process = Runtime.getRuntime()。exec(files.toArray());错误:Runtime类型中的方法exec(String [])也不适用于参数(Object []),我拥有SuperUser Elite版本以保存允许的应用程序... – CELB

+0

您是对的,我错过了论点。我改变了这个问题的路线。 – Luis

+0

确定它看起来像它的尝试同时运行所有命令或权限被拒绝?它的工作正常删除数据与我的方法看看LogCat http://pastebin.com/kv7chv5h – CELB