我启动了一个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);
}
最佳答案
我遇到过同样的问题。这是我解决的方法。
首先,我创建了一个TimerTask来记录/读取logcat或getevent的BufferReader中的连续输出。 TimerTask创建新的线程,该线程在后台运行,并且不会干扰或阻止服务类中的其他UI处理程序。因此,我认为这样更安全。
然后,从onCreate()方法中的Service类启动TimerTask。
一件事是我的设备已经扎根,所以我从“ su”会话开始。我相信“ sh”会话也适用于非根设备。
服务等级:
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);
}
}
}
}