Here's one scheme. NDK is not necessary for this one.
You create a temporary file in your app's data dir. You run the binary, specifying the same file as an output direction in the command line using Runtime.exec()
. The command line you'll have to specify would invoke not the binary itself, but sh
, and one of the command line parameters would be a redirection: >myfile.txt
.
You then start a worker thread that reads the same file. You'll need to carefully implement new data detection - as this is a file, not a pipe, the reading operation will simply terminate once the end of file is reached; it won't block until new data appears.
Then you pass the data from the worker thread using Activity.runOnUiThread()
or Handler.post()
to the main thread, where it's used to update TextView's content.
A cleaner way would involve creating a pipe pair with mkfifo()
and redirecting output in place using dup2()
from a pipe handle to the file handle value 1 (stdout
). That's how they normally do it in the C/Linux world. There's probably an Android example out there, I've never done that.
Oh, and before you do all that, make sure the binary does not have a more palatable interface. More often than not, Linux binaries are based on a static/dynamic library where all the yummy functionality is, one that can be linked with and called directly.
EDIT: Especially if it's logcat
. There are other ways to read a log in Android.