私は実行しようとしています
String command = "su -c 'busybox ls /data'";
p = Runtime.getRuntime().exec(command);
私のアプリでは、構文が何らかの形で間違っているようです。ただし、電話のターミナルエミュレータアプリから実行しても問題ないので、アプリ内から呼び出したときに機能しない理由がわかりません。
どんな助けでも大歓迎です!
解決策が見つかりました! onit hereによって提案されたリンクに感謝します。以下のコードを参照してください: スーパーユーザー シェル コマンドが正しく機能するには、まずスーパーユーザー シェルを作成してプロセスに割り当て、次にその入力ストリームと出力ストリームでそれぞれ書き込みと読み取りを行う必要があります。
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
//from here all commands are executed with su permissions
stdin.writeBytes("ls /data\n"); // \n executes the command
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
String out = new String();
//read method will wait forever if there is nothing in the stream
//so we need to read it in another way than while((read=stdout.read(buffer))>0)
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<BUFF_LEN){
//we have read everything
break;
}
}
//do something with the output
以下の関数を使用します。
public void shellCommandRunAsRoot(String Command)
{
try
{
Process RunProcess= Runtime.getRuntime().exec("su");
DataOutputStream os;
os = new DataOutputStream(RunProcess.getOutputStream());
os.writeBytes(cmds+"\n");
os.writeBytes("exit+\n");
os.flush();
}
catch (IOException e)
{
// Handle Exception
}
}
使用法:
shellCommandRunAsRoot("pkill firefox");