Android WebView 使用总结--持续更新

Android 可以使用webView 来展示网页内容,也可以使用设备自带的浏览器来进行跳转,

具体怎么使用,比较简单,详细不表。

下面记录下当前开发webapp 遇到的问题:

一、使用webView时,如果网页中包含了echarts等显示的图表。

 webSetting的

setting.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

最好不要加。

在Android 4.3以上的版本这么加没有问题,但如果Android版本低于4.3(包含4.3),则网页上图表无法正常显示。


二、webView debug

webView中如果调用了JS,debug时,想要在logcat中看到JS的console .log,可以在WebChromeClient 中实现 onConsoleMessage()方法即可

API level 7 和 API level 8以上,参数不一样,下面是官网的例子:

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;
  }
});

三、webapp打包混淆

在用proguard 对 含有JS调用的webapp 在打包混淆时,需要对@JavaScriptInterface 做特殊申明,否则JS接口将无法正常调用。

proguard-rules.pro中 添加:

-keepattributes *Annotation*
-keepattributes *JavascriptInterface*

-keepclassmembers class com.xx.classJavaScirptInterface {
   public *;
}
 

把com.xx.classJavaScriptInterface 替换成你的类名即可。

附一段官方解释:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

未完待续...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值