13

まず、自分の状況を紹介します。Androidアプリで「su」コマンドを実行する必要がありますが、うまく機能します。次に、「ls」コマンドを実行して出力を読み取る必要があります。私は「su」プロセスから出力ストリームを取得し、それにコマンドを書き込むことによってそれを行っています。

そして、ここに質問があります。「ls」プロセスの出力を読み取る方法は?私が持っているのは「su」プロセスオブジェクトだけです。「su」は何も書き込まないため、そこから入力ストリームを取得しても何も起こりません。しかし、「ls」はそうし、その出力メッセージにアクセスする方法がわかりません。

多くのサイトを検索しましたが、解決策が見つかりませんでした。多分誰かが私を助けてくれるでしょう:)

よろしく

4

3 に答える 3

24

わかりました、私は解決策を見つけました。次のようになります。

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

それが誰かのために役立つことを願っています

于 2011-08-05T08:24:50.940 に答える
5
public String ls () {
    Class<?> execClass = Class.forName("android.os.Exec");
    Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class);
    int[] pid = new int[1];
    FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid);

    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd)));
    String output = "";
    try {
        String line;
        while ((line = reader.readLine()) != null) {
            output += line + "\n";
        }
    }
    catch (IOException e) {}
    return output;
}

ここに記載されているこのコードを確認してください。

Androidアプリケーションでターミナルコマンドを実行するにはどうすればよいですか?


try {
// Executes the command.
Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard");

// Reads stdout.
// NOTE: You can write to stdin of the command using
//       process.getOutputStream().
BufferedReader reader = new BufferedReader(
        new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
    output.append(buffer, 0, read);
}
reader.close();

// Waits for the command to finish.
process.waitFor();

return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

参考文献

このコード GScript

于 2011-08-01T10:12:13.573 に答える
3

次の問題について、@glodosが受け入れた回答を変更しました。

  1. ストリームは閉じられます。閉じられていない場合、開いたストリームでexecプロセスが永久にハングします。psいくつかの実行後にシェル(つまり)で実行すると、いくつかのプロセスが動作しているadb shellことがわかります。suそれらは適切に終了する必要があります。
  2. waitFor()プロセスが確実に終了するように追加されました。
  3. の処理が追加されread=-1、空のコマンドをstdout実行できるようになりました。以前はクラッシュしましたnew String(buffer, 0, read)
  4. StringBufferより効率的な文字列処理のために使用します。

    private String execCommand(String cmd) throws IOException, InterruptedException {
        Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
        DataOutputStream stdout = new DataOutputStream(p.getOutputStream());
    
        stdout.writeBytes(cmd);
        stdout.writeByte('\n');
        stdout.flush();
        stdout.close();
    
        BufferedReader stdin = new BufferedReader(new InputStreamReader(p.getInputStream()));
        char[] buffer = new char[1024];
        int read;
        StringBuffer out = new StringBuffer();
    
        while((read = stdin.read(buffer)) > 0) {
            out.append(buffer, 0, read);
        }
        stdin.close();
        p.waitFor();
        return out.toString();
    }
    

一部のクレジットは@SherifelKhatibに送られます))

于 2017-11-03T00:13:48.060 に答える