如何在点击时在浏览器中的Web视图中加载一些内部链接?

时间:2018-12-19 19:53:37

标签: android webview kotlin android-webview deep-linking

我有一个Web视图,其中在Web视图中有某些链接,这些链接在Web视图中而不是浏览器中打开。我添加了webView.settings.setSupportMultipleWindows(true)  我的代码阻止了链接的完全打开。我想知道是否有一种方法可以通过单击打开浏览器中Web视图中的任何静态链接。有任何想法吗? 我的代码如下:

 webView.settings.javaScriptEnabled = true
        webView.settings.javaScriptCanOpenWindowsAutomatically = true
        webView.settings.setAppCacheEnabled(true)
        webView.settings.setAppCachePath(context?.cacheDir?.absolutePath)
        webView.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        webView.settings.allowFileAccess = true
        webView.settings.allowContentAccess = true
        webView.settings.allowFileAccessFromFileURLs = true
        webView.settings.allowUniversalAccessFromFileURLs = true
        webView.settings.domStorageEnabled = true
        webView.settings.setSupportMultipleWindows(true)

        webView.webChromeClient = WebChromeClient()
        webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
            }

            override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
                super.onReceivedError(view, request, error)
            }

            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                if (request?.url?.toString()?.startsWith("mailto:") == true) {
                    val intent = Intent(Intent.ACTION_VIEW, request.url)
                    startActivity(intent)
                    return true
                }
                if (request?.url?.toString()?.startsWith("tel:") == true) {
                    startActivity(Intent(Intent.ACTION_DIAL, request.url))
                    return true
                }

                return super.shouldOverrideUrlLoading(view, request)
            }
        }

        // Empty cache case
        if (!accessibilityService.isOnline) {
            showErrorMessage(R.string.error_no_network_connection)
        }

        webView.loadUrl(urlToOpen)
        webView.setOnKeyListener(object : View.OnKeyListener {

            override fun onKey(v: View, keyCode: Int, event: KeyEvent): Boolean {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.action === MotionEvent.ACTION_UP
                        && webView.canGoBack()) {
                    webView.goBack()
                    return true
                }

                return false
            }

        })
        toolbar.title = toolbarTitle
        toolbar.setNavigationIcon(R.drawable.ic_transparent)

0 个答案:

没有答案