views:

542

answers:

2

Can I run an Android app through the emulator and make it print strings to my computer's console? By console I mean the standard place you would expect to see a System.out.println() in a normal java application. So if you ran the java application from the command prompt then you will see the println()s in the command prompt or if you ran the program in eclipse you will see it in the Console tab at the bottom.

+4  A: 

Use Log.d("YourTag", "YourOutput");

see http://developer.android.com/reference/android/util/Log.html

Mathias Lin
There are various methods for various importance of logs: Log.v(), Log.d(), Log.i(), etc...Note also that API Level 8 (Android 2.2 Froyo) introduced brand new method Log.wtf() - What a Terrible Failure ;) LOL
Gawcio
+3  A: 

By default, the Android system sends stdout and stderr (System.out and System.err) output to /dev/null. In processes that run the Dalvik VM, you can have the system write a copy of the output to the log file. In this case, the system writes the messages to the log using the log tags stdout and stderr, both with priority I.

To route the output in this way, you stop a running emulator/device instance and then use the shell command setprop to enable the redirection of output. Here's how you do it:

$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start

The system retains this setting until you terminate the emulator/device instance. To use the setting as a default on the emulator/device instance, you can add an entry to /data/local.prop on the device.

You can find more information on this in the Android Debug Bridge document.

You could also create your own class to print to the console http://tech.chitgoks.com/2008/03/17/android-showing-systemout-messages-to-console/

I think this question has been answered on StackOverflow already http://stackoverflow.com/questions/2571617/how-to-output-logcat-to-console

Ally