2012-09-29 97 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