android 基于jsBridge实现js交互时对webview监听onPageStarted及onPageFinished

来源:互联网 发布:java格式化日期24小时 编辑:程序博客网 时间:2024/06/10 23:04

最近Android项目中需要实现webview与js交互,网上推荐的框架是https://github.com/lzyzsd/JsBridge
基于jsbridge调用实现webview与js交互具体用法作者已详细讲解不再解释,本文主要说在实现webview交互后想监听webview的onPageStarted及onPageFinished实现加载动画的坑。
js交互代码

//  JS调JAVA   重点: Java端需要注册事件监听,即webView.registerHandler()。PHP需要做的工作:重写接口回调,        webView.registerHandler("goodsInfo", new BridgeHandler() {            @Override            public void handler(String data, CallBackFunction function) {                Logger.json(data);                Map<String, Object> map = JSONFormat.jsonToMap(data);                String param = (String) map.get("goodsid");                aCache.put("goodsname", map.get("goodsname") + "");                aCache.put("goodsimg", "http://" + URLutile.PHPURL + "/" + map.get("goodsimg") + "");                Intent intent = new Intent(HelpMallActivity.this, GoodsDetailActivity.class);                intent.putExtra("param", param);                startActivity(intent);            }        });

交互后重点是监听webview的onPageStarted及onPageFinished实现加载动画
用的代码是

webView.setWebViewClient(new WebViewClient()        {            @Override            public void onPageFinished(WebView view, String url)            {                super.onPageFinished(view, url);                stopMyDialog();            }            @Override            public void onPageStarted(WebView view, String url, Bitmap favicon)            {                super.onPageStarted(view, url, favicon);                startMyDialog();            }        });

后发现js交互不能用了,开始查看jsBridge源码发现BridgeWebViewClient中已经重新写了onPageStarted及onPageFinished的监听,两个冲突了,js交互也就不能用了。

解决办法
新建一个WebViewClient继承BridgeWebViewClient

package com.yskj.lsk.widget.circleimageview;import android.content.Context;import android.graphics.Bitmap;import android.webkit.WebView;import com.github.lzyzsd.jsbridge.BridgeWebView;import com.github.lzyzsd.jsbridge.BridgeWebViewClient;import com.yskj.lsk.util.MyLoading;/** * Created by jianghe on 2016/11/18 0018. */public class LocalWebViewClient extends BridgeWebViewClient {    private MyLoading myloading;    private Context context;    public LocalWebViewClient(BridgeWebView webView) {        super(webView);    }    public LocalWebViewClient(BridgeWebView webView, Context context){        super(webView);        this.context = context;    }    @Override    public void onPageStarted(WebView view, String url, Bitmap favicon) {        super.onPageStarted(view, url, favicon);        startMyDialog();    }    @Override    public void onPageFinished(WebView view, String url) {        super.onPageFinished(view, url);        stopMyDialog();    }    /**    * dialog 启动    */    public void startMyDialog() {        if (myloading == null) {            myloading = MyLoading.createLoadingDialog(context);        }        if (!myloading.isShowing()) {            myloading.show();        }    }    /**     * dialog 销毁     */    public void stopMyDialog() {        if (myloading != null) {            if (myloading.isShowing()){                myloading.dismiss();            }            myloading = null;        }    }}

再在web中调用
//监听加载开始和加载结束
webView.setWebViewClient(new LocalWebViewClient(webView,context));
后一切正常

0 0