WebView

一、 初始WebView
1、wb.getSettings().setJavaScriptEnabled(true); // 支持网页中的js 效果。

2、 WebViewClient :
官方文档上的解释:Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions.

wb.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
重写方法:shouldOverrideUrlLoading(WebView view, String url) :
官方文档解释:Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST “method”
意思就是。 给你的应用程序一次机会去接管当一个新的url 被登录在你的webView 。 如果你的WebViewClient 没有被提供, 默认的 webVIew 会ask Activity 管理器 来选择手机上带的浏览器就行打开。如果你提供了webVIewClient ,如果你返回结果为true ,则意味着你要的程序接管当前的url 。 如果你返回结果为false,则意味着 你要当前的webView 自己处理要发生的事。

3、WebChromeClient
官方文档api23:Creating and setting a WebChromeClient subclass. This class is called when something that might impact a browser UI happens, for instance, progress updates and JavaScript alerts are sent here (see Debugging Tasks).
大致意思就是 : 当你提供了webChromeClient 的时候 。你可以根据进度的改变做一些事情 重写onProgressChanged(WebView view, int newProgress)。

4、 webVIew js 交互 (目前只用到了 网页去调用 安卓端的方法。)
首先 需要设置 webView .addJavaScriptInterface(Object obj,String name)。
官方文档:Injects the supplied Java object into this WebView. The object is injected into the JavaScript context of the main frame, using the supplied name. This allows the Java object’s methods to be accessed from JavaScript. For applications targeted to API level JELLY_BEAN_MR1 and above, only public methods that are annotated with JavascriptInterface can be accessed from JavaScript. For applications targeted to API level JELLY_BEAN or below, all public methods (including the inherited ones) can be accessed, see the important security note below for implications.
意思就是 。你需要提供一个对象可以供网页端去调用。 需要注意的是api版本小于17, 定义的类中的所用public方法(包括继承的方法)都可以被调用。 大于等于api17 需要在public方法上添加注解 @JavascriptInterface
方法第一个参数代表你写的类的对象。 你要在此类中写一些供 网页端调用的方法。 第二个参数则是你的对象的名字。eg : wb.addJavaScriptInterface(this,”android”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值