有没有办法在 Android 应用程序中打印到控制台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3102443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:41:53  来源:igfitidea点击:

Is there a way to print to the console in an Android app?

androidprintln

提问by Anton

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.

我可以通过模拟器运行 Android 应用程序并使其将字符串打印到我的计算机控制台吗?我所说的控制台是指您希望在普通 Java 应用程序中看到 System.out.println() 的标准位置。因此,如果您从命令提示符运行 java 应用程序,那么您将在命令提示符中看到 println() 或者如果您在 eclipse 中运行该程序,您将在底部的控制台选项卡中看到它。

回答by Mathias Conradt

回答by Ally

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.

默认情况下,Android 系统会将 stdout 和 stderr(System.out 和 System.err)输出发送到 /dev/null。在运行 Dalvik VM 的进程中,您可以让系统将输出的副本写入日志文件。在这种情况下,系统使用日志标签 stdout 和 stderr 将消息写入日志,两者都具有优先级 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:

要以这种方式路由输出,请停止正在运行的模拟器/设备实例,然后使用 shell 命令 setprop 启用输出重定向。以下是您的操作方法:

$ 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.

系统会保留此设置,直到您终止模拟器/设备实例。要将设置用作模拟器/设备实例上的默认设置,您可以向设备上的 /data/local.prop 添加一个条目。

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

您可以在Android Debug Bridge 文档中找到更多相关信息。

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/

您还可以创建自己的类以打印到控制台 http://tech.chitgoks.com/2008/03/17/android-showing-systemout-messages-to-console/

I think this question has been answered on StackOverflow already How to output LogCat to Console?

我认为这个问题已经在 StackOverflow 上得到了解答 How to output LogCat to Console?