Using standard input and output on Android [closed]

-1

When I learned to program in Java, I used a lot of standard input and output (stdin and stdout). Now that I am curious to learn what I can do on Android, I wanted to know if for practicality I can do the same thing I did on the Java SE platform, which is: create a "console" application.

With a console application we can do some very practical things like:

Direct the output of the program to other Unix utilities (on Android requires Busybox).

am start -n com.example.android/.MainActivity | sort -u | sed ... > saída.txt

Or direct the output of other programs to my program's default input.

logcat|am start -n com.example.android/.MainActivity

I got to try out the Jackpal Terminal Emulator and I noticed that it uses these features:

  

Android absolutely supports pipes as well as unix domain sockets. Use   of exec is somewhat discouraged, but works at the moment.

     

See the source of any android terminal emulator with a local shell   an example of how to do it. Essentially your gui just   replaces the terminal emulator, and your engine replaces the shell.    Chris Stratton

See also

asked by anonymous 28.04.2016 / 19:01

1 answer

2

As the SOen a way to get the console data would be:

// Create a stream to hold the output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);

// IMPORTANT: Save the old System.out!
PrintStream old = System.out;

// Tell Java to use your special stream
System.setOut(ps);

System.out.println("Foofoofoo!");
// Put things back
System.out.flush();
System.setOut(old);

baos.toString(); //Pega os dados

Then get this baos.toString(); and put it in a TextField or similar.

You can also change System.out.print by a function of your own that would make an append in TextField.

    
28.04.2016 / 19:40