简体   繁体   English

HTC Android设备上的Javascript console.log()和adb logcat

[英]Javascript console.log() on HTC Android devices and adb logcat

I am developing the application in HTML which is calling the console.log() from Javascript to provide me logs during the development about what happens in the web page code. 我正在用HTML开发应用程序,它从Javascript调用console.log() ,在开发期间为我提供关于网页代码中发生的事情的日志。

Unfortunately when I use the adb logcat command to check logs I can see output from all other applications, but not the output from my JavaScript code. 不幸的是,当我使用adb logcat命令检查日志时,我可以看到来自所有其他应用程序的输出,但不能看到我的JavaScript代码的输出。 I can see even the log from web browser that the page is loaded, but not console.log() output from my JavaScript code executed in the web browser. 我甚至可以从Web浏览器看到加载页面的日志,但不能看到我在Web浏览器中执行的JavaScript代码输出的console.log()

According to information on this page ( http://developer.android.com/guide/webapps/debugging.html ) it should work. 根据此页面上的信息( http://developer.android.com/guide/webapps/debugging.html ),它应该可以工作。

I am testing on HTC WildFire and HTC Desire HD . 我正在测试HTC WildFireHTC Desire HD


Edited after more then 6 months 超过6个月后编辑

After some time and experience with different devices (phones, TVs, set top boxes, WebViews, UIWebViews...) my advice is to do the remote logging from JavaScript and not relying on the console.log() or other methods - see the nice trick with the image loading here . 经过一段时间和使用不同设备(手机,电视,机顶盒,WebViews,UIWebViews ......)的经验,我的建议是从JavaScript进行远程日志记录而不依赖于console.log()或其他方法 - 请参阅好的技巧与图像加载在这里

Do not miss the presentation here Hope this helps! 不要错过这里的演示希望这有帮助! STeN 斯登

In the default browser on Android 2.3.3 (and presumably from then on) you can simply use the built in javascript console: 在Android 2.3.3的默认浏览器中(可能从那时开始),您可以简单地使用内置的JavaScript控制台:

  • open the browser 打开浏览器
  • go to your webpage 转到您的网页
  • type about:debug in the address bar and hit enter 键入about:debug在地址栏中about:debug并按Enter键
  • you'll see a section for debug options appear if you go into the browser app's settings 如果您进入浏览器应用程序的设置,您将看到有关调试选项的部分
  • tick "Show JavaScript Console" if not already enabled 勾选“显示JavaScript控制台”(如果尚未启用)
  • refresh your webpage 刷新您的网页

At the top, you'll see a bar labeled "JavaScript Console". 在顶部,您会看到标有“JavaScript控制台”的栏。

I have been using three different HTC phones, almost exclusively, and have never had this issue. 我一直在使用三种不同的HTC手机,几乎完全没有这个问题。 Here are a few things to check: 以下是一些要检查的事项:

  1. Make sure USB debugging is enabled. 确保已启用USB调试。
  2. Make sure your Webview has a WebChromeClient set. 确保您的Webview具有WebChromeClient集。 The browser used in Webview does not implement console.log(). Webview中使用的浏览器不实现console.log()。
  3. It should be easy to see the output of adb logcat, but to make it easier, filter the output. 应该很容易看到adb logcat的输出,但为了更容易,过滤输出。

Turn on USB debugging: 打开USB调试:

  1. Disconnect your device from your computer. 断开设备与计算机的连接。
  2. Go to Settings -> Applications -> Development -> Select "Enable USB Debugging" 转到设置 - >应用程序 - >开发 - >选择“启用USB调试”
  3. Plugin to computer. 插件到电脑。 (Make sure you have the correct drivers installed to use ADB - more info here: http://developer.android.com/guide/developing/device.html ) (确保安装了正确的驱动程序以使用ADB - 更多信息请访问: http//developer.android.com/guide/developing/device.html

Set a WebChromeClient that overrides onConsoleMessage(): 设置一个覆盖onConsoleMessage()的WebChromeClient:

//Set the output prefix so you can filter adb logcat results later
public static final String TAG = "Your-Application-Name";

myWebView = (WebView) findViewById(R.id.webview);

//Not going to have much luck running JS without this:
myWebView.getSettings().setJavaScriptEnabled(true);

//Override onConsoleMessage() to output to the Log.
myWebView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onConsoleMessage(ConsoleMessage cm) {
        Log.d(TAG, cm.message() + " -- From line "
        + cm.lineNumber() + " of "
        + cm.sourceId() );
        return true;
    }
});

More info on onConsoleMessage() here: 有关onConsoleMessage()的更多信息: http://developer.android.com/reference/android/webkit/WebChromeClient.html#onConsoleMessage(java.lang.String , int, java.lang.String) http://developer.android.com/reference/android/webkit/WebChromeClient.html#onConsoleMessage(java.lang.String,int,java.lang.String

More info on debugging in general here: http://developer.android.com/guide/webapps/debugging.html 有关调试的更多信息,请访问: http//developer.android.com/guide/webapps/debugging.html

Filter the output of adb logcat: 过滤adb logcat的输出:

adb logcat tag-name:log-level *:S

tag-name matches the string specified in Log.x log-level matches the log level you indicated when calling Log.x <--- tag-name与Log.x日志级别中指定的字符串匹配,与调用Log.x时指示的日志级别相匹配<---

Example relating to code above: 与上述代码有关的示例:

adb logcat Your-Application-Name:D *:S

This will show all d level logs for the matching tag, Your-Application-Name, and silence everything else. 这将显示匹配标记的所有d级日志,Your-Application-Name,并使其他所有内容静音。

More info on adb filtering here: http://developer.android.com/guide/developing/tools/adb.html#logcat 有关adb过滤的更多信息,请访问: http//developer.android.com/guide/developing/tools/adb.html#logcat

Hope it helps! 希望能帮助到你! I know it was a bit of summarizing of the other answers, but, the fact is, it takes all of the steps to make it work. 我知道这是对其他答案的总结,但事实是,它需要所有步骤才能使其发挥作用。 :) :)

I had the same problem, I solve it by doing the following: 我有同样的问题,我通过执行以下操作解决它:

1/ when you initialize your webview, add a javascript bridge class: 1 /初始化webview时,添加一个javascript桥类:

appView.addJavascriptInterface(new JSBridge(), "JSBridge");

2/ create the class JSBridge with a log function 2 /使用日志功能创建JSBridge类

class JSBridge {
  public void log(String msg){
   Log.d(msg);
  }
}

3/ in your javascript you can now call 3 /在您的javascript中,您现在可以拨打电话

JSBridge.log("my log message");

Might be a bit overkill for your problem, but you can also do a LOT more with that solution 可能对您的问题有点矫枉过正,但您也可以使用该解决方案做更多事情

Logcat generates a lot of stuff on devices in addition to yours so you may have to filter this. 除了你的设备之外,Logcat还会在设备上生成大量内容,因此您可能必须对此进行过滤。

You can try to "grep" your log output if you've tagged it. 如果您标记了日志输出,可以尝试“grep”它。 For example: acording to your article the output should look like: 例如:根据您的文章,输出应如下所示:

Console: Hello World http://www.example.com/hello.html :82

If you use the command (assuming you are using a Linux, Mac OS or anything else with a grep command) 如果您使用该命令(假设您使用的是Linux,Mac OS或其他任何带有grep命令的命令)

adb logcat | grep -i "console"

it will reduce the output to the keyword specified within these quotation marks. 它会将输出减少到这些引号内指定的关键字。 If you add a unique tag to your output you can also filter this so you get only things you want to see. 如果您在输出中添加唯一标记,则还可以对此进行过滤,以便只获得您想要查看的内容。

[ctrl]+c

will stop this logcat process. 将停止此logcat进程。

See this: How to display console.log() output in PhoneGap app using Eclipse and HTC Desire HD? 请参阅: 如何使用Eclipse和HTC Desire HD在PhoneGap应用程序中显示console.log()输出?

It seems that console.log is disabled on HTC devices running Android 2.2. 似乎在运行Android 2.2的HTC设备上禁用了console.log。

You can get around it by using remote debugging in jsconsole.com . 您可以通过在jsconsole.com中使用远程调试来解决它。

Try the following: 请尝试以下方法:

1) Open the devices tab and make sure that the device you have connected is selected/highlighted. 1)打开设备选项卡,确保已选择/突出显示已连接的设备。

2) Make sure USB Debugging is enabled on your device. 2)确保您的设备启用了USB调试。 Here is what you need to do for this: 以下是您需要做的事情:

2a) From the Home screen, press MENU, and then tap Settings > Applications > Development. 2a)在主屏幕中,按MENU,然后点击设置>应用程序>开发。

2b) Make sure the USB debugging check box is selected. 2b)确保选中USB调试复选框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM