2011-07-04 60 views
2

我正在寫一個文件資源管理器,它將能夠修改系統文件的根訪問權限,但我遇到了一些問題。在文件資源管理器的根訪問

我現在正在做的事是授予我的應用程序根權限,但執行「su」不起作用。 如果我將權限設置爲adb shell中的文件夾,該應用程序工作正常,但我認爲根瀏覽不依賴於chmods。

任何人都可以告訴我有沒有適當的方法讓我的應用程序工作,就好像它是根特權?

回答

2

以root身份運行android應用程序進程(其dalvik虛擬機和本機庫)非常難以實現,並且由於許多原因而不受歡迎,包括安全性,但由於必須加載系統庫的私有副本而導致內存浪費在正常應用程序啓動時從zygote繼承非特權進程時使用可用的共享只讀副本。

在某些根植電話上進行的非官方「su」攻擊可以讓您啓動以root身份運行的幫助程序進程,而您的應用程序進程仍處於非特權狀態。它不會改變調用它的應用程序的用戶標識 - 實際上,在unix類操作系統上確實沒有設計任何機制來做到這一點。

一旦你有一個特權輔助進程,你需要通過一些進程間通信方式(比如它的標準輸入/標準輸出或者unix域套接字)與它通信,讓它代表你進行文件操作。手機上的shell甚至可能被用作助手應用程序 - 文件管理器需要做的大部分操作都可以通過'cat'命令來實現。官方沒有一個穩定的API,但是隨後一個應用程序可訪問的「su」破解程序不在官方的android中,因此整個項目深入到「不受支持」的領域。

+0

好吧,我明白了。我希望有一個更簡單的方法,但無論如何謝謝你的提示。 – glodos

1

根訪問會創建一個新的進程,因此,您的應用程序沒有root權限。
您可以使用root權限執行的獨特功能是執行命令,因此,您必須知道android的命令,許多命令都基於Linux,如cp,ls等。

使用此代碼執行命令,並得到輸出:

/** 
* Execute command and get entire output 
* @return Command output 
*/ 
private String executeCommand(String cmd) throws IOException, InterruptedException { 
    Process process = Runtime.getRuntime().exec("su"); 
    InputStream in = process.getInputStream(); 
    OutputStream out = process.getOutputStream(); 
    out.write(cmd.getBytes()); 
    out.flush(); 
    out.close(); 
    byte[] buffer = new byte[1024]; 
    int length = buffer.read(buffer); 
    String result = new String(buffer, 0, length); 
    process.waitFor(); 
    return result; 
} 

/** 
* Execute command and an array separates each line 
* @return Command output separated by lines in a String array 
*/ 
private String[] executeCmdByLines(String cmd) throws IOException, InterruptedException { 
    Process process = Runtime.getRuntime().exec("su"); 
    InputStream in = process.getInputStream(); 
    OutputStream out = process.getOutputStream(); 
    out.write(cmd.getBytes()); 
    out.flush(); 
    out.close(); 
    byte[] buffer = new byte[1024]; 
    int length = buffer.read(buffer); 
    String output = new String(buffer, 0, length); 
    String[] result = output.split("\n"); 
    process.waitFor(); 
    return result; 
} 

用法的文件瀏覽器:文件

獲取列表:

for (String value : executeCmdByLines("ls /data/data")) { 
    //Do your stuff here 
} 

閱讀文本文件:

String content = executeCommand("cat /data/someFile.txt"); 
//Do your stuff here with "content" string 

複製文件(cp命令不工作在某些設備上):

executeCommand("cp /source/of/file /destination/file"); 

刪除文件(rm命令不工作在某些設備上):

executeCommand("rm /path/to/file");