ルートアクセスで新しいプロセスが作成されるため、アプリケーションにルート権限がありません。
root特権でできることは実行コマンドなので、アンドロイドのコマンドを知る必要があります。多くのコマンドはcp
、ls
などのLinuxベースです。
使用するコマンドを実行し、出力を得るために、このコード:ファイルエクスプローラのため
/**
* 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");
[OK]を、私は理解しています。私はより簡単な方法があったが、とにかくヒントをありがとう。 – glodos