To run the executable and get the real-time output I'm using the following code (with String[] command = new String[]{"/a.out"};
):
public static void execute(String... command) {
try {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
final Process proc = builder.start();
new Thread(new Runnable() {
public void run() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
EventBus.getDefault().post(new MessageEvent(line+"\n"));
}
} catch (Exception e) {
// TODO
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
}
The command should be executed inside a service:
public class Service extends IntentService {
...
@Override
protected void onHandleIntent(Intent intent) {
String[] command = new String[]{"/a.out"};
Class.execute(command);
}
}
But I only get one line as an output, nothing more, but it should be hundreds of them. I already tried it on my computer by replacing EventBus.getDefault().post(new MessageEvent(line+"\n"));
with System.out.println(line);
and it works just fine.
Additional information: Subscriber handles EventBus through:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
tv.append(event.getMessage());
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
And tv is a TextView: TextView tv = (TextView)findViewById(R.id.TextView);
This structure was tested with a simple method, which in the following shown. It updated the TextView and there was more than one line passed to the TextView. So I guess it must be something inside my execution method.
public static void test() {
for(int i = 0; i<500000; i++)
EventBus.getDefault().post(new MessageEvent(i+"\n"));
}
Why does the EventBus only passes one line? What can I change to get the expected result?
Edit: Changing onMessageEvent()
to:
public void onMessageEvent(MessageEvent event) {
Log.i("Activity",event.getMessage());
}
Also just one line.
Tested it with another executable, now it works just fine, any idea why it works now?