美文网首页
webView的基本使用

webView的基本使用

作者: 阳光下的美好_6e13 | 来源:发表于2018-12-14 17:19 被阅读6次
WebUtil.syncCookie(viewModel.webUrl.get(), SharedPreferencesUtils.getProperty(this, "cookice"));
        final WebSettings webSettings = binding.webView.getSettings();
        // 设置与Js交互的权限
        webSettings.setJavaScriptEnabled(true);
        binding.webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                return super.onConsoleMessage(consoleMessage);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    binding.progressBar.setVisibility(View.INVISIBLE);//加载完网页进度条消失
                } else {
                    binding.progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    binding.progressBar.setProgress(newProgress);//设置进度值
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                //获取网页标题一定要在这里获取,才能预先加载
                if (news.equals("1")) {
                    viewModel.title.set("资讯");
                } else {
                    viewModel.title.set(view.getTitle());
                }
            }
        });
        // 绑定webview,支持h5埋点的关键步骤
        // 如需支持h5统计,需要将 assets/mobstat.js 拷贝到工程目录下
        //StatService.bindJSInterface(this, binding.webView);

        binding.webView.addJavascriptInterface(new NewsInfoActivity.RemoteService(this), interfaceName);//AndroidtoJS类对象映射到js的test对象

        binding.webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
             /* 不要在这里获取加载,这里是页面加载完成后才会显示标题
                if (news.equals("1")) {
                    viewModel.title.set("资讯");
                } else {
                    viewModel.title.set(view.getTitle());
                }*/
                viewModel.webUrl.set(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                viewModel.title.set(view.getTitle());
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                binding.webView.setVisibility(View.GONE);
            }

            @Override
            public void onPageStarted(WebView view, final String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Document doc = Jsoup.connect(url).get();
                            viewModel.shareTitle.set(doc.getElementsByClass("new_title_line").text());
                            viewModel.shareImg.set(doc.getElementsByClass("newsPreviewImg").text());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });
    }

相关文章

网友评论

      本文标题:webView的基本使用

      本文链接:https://www.haomeiwen.com/subject/lnexqqtx.html