2012-02-15 7 views
2

多分IMOではなく、アンドロイドとの接続があります。 Javaのサブプロセスの出力を入手するにはどうすればいいですか

package com.main.app; 

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Scanner; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.os.Bundle; 
import android.widget.TextView; 

public class RootPermissionActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     try { 
      Process p = Runtime.getRuntime().exec("su"); 
      DataOutputStream outputStream = new DataOutputStream(p.getOutputStream()); 
      outputStream.writeBytes("touchasd /m\n"); 

         //codes below used to get the error output, but get nothing. 
      TextView suCommandMessage = (TextView)findViewById(R.id.suCommandMessage); 
      BufferedReader d = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
       String msg; 
       while((msg = d.readLine()) != null) { 
        suCommandMessage.setText(msg); 
       } 

      outputStream.writeBytes("exit \n"); 
      p.waitFor(); 
     } catch(IOException e) { 
      new AlertDialog.Builder(this) 
          .setTitle("Exception") 
          .setMessage("IOException: " + e) 
          .setPositiveButton("OK", null) 
          .show(); 
     } catch(InterruptedException e) { 
      new AlertDialog.Builder(this) 
       .setTitle("Exception") 
       .setMessage("InterruptedException: " + e) 
       .setPositiveButton("OK", null) 
       .show(); 
     } 
    } 
} 

は最初私はそれだけでシェルコマンドを実行するためのサブプロセスだ、root権限を得るために新しいプロセス suを作成し、その後、私はサブプロセスにはいくつかのコードを書き、すべてがOKです。私はサブプロセスのエラー出力を取得したいので、間違ったコマンドを書く touchasd /m\n、しかし、私はサブプロセスのエラー出力を得る方法を知らない、誰も私に手を差し伸べることができますか?どうも。 :)

答えて

0

プロセスのエラーストリームを取得するには、次のようにします。

BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream())); 

あなたのコードブロック内のコメントは、コードブロック内でコメントされているようです。

+0

ああ、私が最初に書いたことはあなたを誤解させるかもしれない。私はちょうど今それを編集しました。実際に、いくつかのコードは、彼らが仕事をしていないことを示すだけであるとコメントされています。 – Searene

関連する問題