Android不断获得流程输出

时间:2017-01-04 22:37:23

标签: java android process inputstream

我有一个Android进程,我开始。这是一个getevent命令。这(在控制台中运行时)会发生连续的事件发生。我希望在Android应用中收集这些内容。我目前这样做的方式将有效地收集"一个关闭"过程输出,但我似乎无法找到一种方法来存储getevent方法发生的连续结果。

我现有的代码如下。它适用于像" ls"但不适用于连续流,例如" getevent"。

try {
            Process chmod = Runtime.getRuntime().exec("getevent -lt /dev/input/event1");

            BufferedReader reader = new BufferedReader(new InputStreamReader(chmod.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();
            chmod.waitFor();
            String outputString =  output.toString();
            Log.d("output", outputString);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

1 个答案:

答案 0 :(得分:1)

我有同样的问题。以下是我解决它的方法。

首先,我创建了一个TimerTask来记录/读取logcat或getevent的BufferReader中的连续输出。 TimerTask创建新的Thread,它在后台运行,不会干扰或阻止服务类中的其他UI处理程序。所以我认为这样更安全。

然后我在onCreate()方法中从Service类启动了TimerTask。

有一件事是我的设备扎根了,所以我从'su'会话开始。我相信'sh'会话也适用于非root设备。

服务类:

public class ServiceClassPhone extends Service {
....

private GetEventRecorder getEventRecorder;

@Override
public void onCreate() {

....

getEventRecorder= new GetEventRecorder();
getEventRecorder.start();
// (if there is any) postDelay other UI handlers in service
}

和GetEventRecorder类:

public class GetEventRecorder {


....
private static Logger mLogger = new Logger();

private GetEventRecorder mRecorder = null;
Timer timer;

// active su session
private Process mProcess;
// Byte Writer
private OutputStream mStdIn;
private DataOutputStream outputStream;
private BufferedReader br;


{
    try {
        mProcess = Runtime.getRuntime().exec("su");

        outputStream = new DataOutputStream(mProcess.getOutputStream());
        String comm1 = "getevent -l";
        String comm2 = "logcat -c";
        String close = "^C";
        String newLine= "\n";

        outputStream.writeBytes(comm1);
        outputStream.writeBytes(newLine);


    } catch (IOException e) {
        Log.d(TAG, "Could not spawn su process");
        e.printStackTrace();
    }
}


public void start()  {

        try {

            timer = new Timer();
            timer.scheduleAtFixedRate(new GetEventRecorder.RecorderTask(), 0, 1000);
        } catch (Exception e) {
            Log.d(TAG, "Exception: " + e);
        }

}

public void stop() {
    if (mRecorder != null) {
        mRecorder.stop();
        mRecorder = null;
        timer.cancel();
    }
}



public void logGetEventData(){
    ....

}

}


    private class RecorderTask extends TimerTask {


    public RecorderTask() {}


    @Override
    public void run() {

        try {

            String comm1 = "getevent -l";
            String comm2 = "logcat -c";
            String close = "^C";
            byte[] newLine = "\n".getBytes();

            outputStream.writeBytes(close);
            outputStream.flush();


            br = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
            boolean cont=true;
            String line;
            Log.d(TAG,"geteventLogs BufferedReader for continuous getevent reading... " );
            if (br!=null ) {
                Log.d(TAG,"geteventLogs BufferedReader is not null checking for readiness... ");
                if (br.ready()) {
                    Log.d(TAG, "BufferedReader for getevent is not null and ready");


                    String separator = System.getProperty("line.separator");
                    while ((line = br.readLine()) != null && line.contains("event0:")
                            && !line.contains("BufferedReader") ) {
                            ...
                            logGetEventData();

        }catch (Exception e) {
            if (DBG) Log.d(TAG, "getevent recorder error: " + e);
        }
    }
}


}