android-Debugging Web Apps

If you are testing your web app with a device running Android 4.4 or higher, you can remotely debug your web pages in WebView with Chrome Developer Tools, while continuing to support older versions of Android.

The format of the message might appear different depending on which version of Android you're using. On Android 2.1 and higher, console messages from the Android Browser are tagged with the name "browser". On Android 1.6 and lower, Android Browser messages are tagged with the name "WebCore".

Android's WebKit does not implement all of the console APIs available in other desktop browsers. 

》  All the console APIs shown above are also supported when debugging in  WebView . If you're targeting Android 2.1 (API level 7) and higher, you must provide a  WebChromeClient  that implements the  onConsoleMessage() method in order for console messages to appear in logcat. Then, apply the  WebChromeClient  to your  WebView with  setWebChromeClient() .

For example, to support API level 7, this is how your code for onConsoleMessage(String, int, String) might look:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient() {
  public void onConsoleMessage(String message, int lineNumber, String sourceID) {
    Log.d("MyApplication", message + " -- From line "
                         + lineNumber + " of "
                         + sourceID);
  }
});

However, if your lowest supported version is API level 8 or higher, you should instead implementonConsoleMessage(ConsoleMessage). For example:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient() {
  public boolean onConsoleMessage(ConsoleMessage cm) {
    Log.d("MyApplication", cm.message() + " -- From line "
                         + cm.lineNumber() + " of "
                         + cm.sourceId() );
    return true;
  }
});

Whether you're using onConsoleMessage(String, int, String) or onConsoleMessage(ConsoleMessage), when you execute a console method in your web page, Android calls the appropriate onConsoleMessage()method so you can report the error. For example, with the example code above, a logcat message is printed that looks like this:

Hello World -- From line 82 of http://www.example.com/hello.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值