Using standard input and output on Android

Asked

Viewed 237 times

-1

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

With a console app we can do practical things like:

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

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

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

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

I even tried the Jackpal Terminal Emulator and realized 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 option for 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

  • But using Androidstudio or Androidclipse System.out.print shows in the terminal of the IDE itself the output for you to debug and if you have the cable connected to the mobile phone and the drivers properly installed for the device you can install your app in development directly on the mobile and the output will return to the IDE, even if it’s running on the phone. Another thing the ADT has emulator. -- To be honest I didn’t understand your question.

  • Thank you for the @Guilhermenascimento remark, so that I am aware of. But for example, if I go out with my Smartphone, I’m on the street and I want to leave an app hanging in the background and also want to see its output? I wanted a suggestion on how I can view logs on the Smartphone itself.

  • then you would have to create a text field in the app itself and swap System.out by a textfield append method and the background would run on a trhead

  • if I accept an answer others can still answer or I have to uncheck the answer? @Guilhermenascimento

  • @guiwp a question may have innumerable answers, but only one accepted. And that doesn’t stop others from posting answers. But in this case, it is no longer possible to answer, because it was closed as pending. See the guidelines in the yellow box and, if you have already made the requested changes, wait until it is reopened.

  • Thanks @Diegof, I made the changes clarifying my question better by putting more details and even changing the title of the question. Anything to make it clearer.

  • Others can answer yes, even if you already have an answer accepted, but I disagree with your issue, when so ask a new question. I’m gonna do a rollback because you changed the context of what you were asking.

  • Diegof, @Guilhermenascimento, if you want I ask you to remove this question so I can do another one in more detail (not today, another day)! Anyway thank you very much, I’m learning to move it from here! hug!

  • 1

    Do not need to remove to ask a new question, understand that the context here has been answered, just make a new one as if it were a sequence of this. I myself ask several questions, had some about [tag:ngnix] that were a need sequence of the other, of course you can edit. Just a tip, we’re not a forum, we’re a Q&A ;)

  • I liked this "we are not a forum, we are a Q&A ;)" kkkk. Everything ok friend, I hope to better understand this business here not to work for you!

  • 1

    I reverted the question to the original and for you not to have work here is your text to ask a new question http://answall.com/revisions/125795/5

  • today I took a look at the subject and learned new things and decided to share updating the body of the text

Show 7 more comments

1 answer

2

According to the Soen a way to grab 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 take this baos.toString(); and place in a Textfield or similar.

You can also change System.out.print by an own function that would make an append in Textfield.

  • 2

    It seems an interesting alternative, I will study it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.