Android Web 视图不显示共享表

发布于 2025-01-18 11:12:50 字数 4127 浏览 1 评论 0原文

我正在使用 Android webView 显示 html 页面,我的页面上有一个共享选项。 当网页加载到 Android WebView 并点击共享按钮时没有任何反应,我如何在 Android Web 视图中显示共享表

     viewBinding.webView.apply {
    
                setBackgroundResource(R.drawable.window_bg);
                setBackgroundColor(android.graphics.Color.TRANSPARENT)
    
                settings.apply {
                    javaScriptEnabled = true
                    builtInZoomControls = false
                    displayZoomControls = false
                    allowFileAccess = true
                    allowContentAccess = true
                    domStorageEnabled = true
        
                }
            }

  viewBinding.webView.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                viewBinding.webView.visibility = View.GONE
                viewBinding.progressBar.visibility = View.GONE
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                viewBinding.progressBar.visibility = View.GONE
                viewBinding.webView.visibility = View.VISIBLE
            }

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


            override fun shouldOverrideUrlLoading(webView: WebView, url: String): Boolean {
                if (url.startsWith("http"))
                    return false //open web links as usual

                if (url.startsWith("intent:")) {
                    val intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME)
                    val data = intent.data
                    data?.let {
                        try {
                            val scheme = data.scheme
                            if (FACEBOOK_APP_SCHEMA == scheme) {
                                startActivity(intent)
                                return true
                            }
                        } catch (e: ActivityNotFoundException) {
                            val marketIntent = Intent(Intent.ACTION_VIEW).setData(
                                Uri.parse("$PLAY_STORE_URL${intent.getPackage()}")
                            )
                            if (marketIntent.resolveActivity(packageManager) != null) {
                                [email protected](marketIntent)
                                return true
                            }
                        }
                    }

                }
                return false
            }
        }

        viewBinding.webView.webChromeClient = object : WebChromeClient() {
            override fun onPermissionRequest(request: PermissionRequest?) {
                request?.grant(request.resources);
            }

           
            override fun onShowFileChooser(
                webView: WebView?,
                filePathCallback: ValueCallback<Array<Uri>>?,
                fileChooserParams: FileChooserParams?
            ): Boolean {
                uploadMessage?.onReceiveValue(null)
                uploadMessage = null

                uploadMessage = filePathCallback

                val intent = fileChooserParams!!.createIntent()
                try {
                    startActivityForResult(intent, REQUEST_SELECT_FILE)
                } catch (e: ActivityNotFoundException) {
                    uploadMessage = null
                    Toast.makeText(
                        applicationContext,
                        "Cannot Open File Chooser",
                        Toast.LENGTH_LONG
                    ).show()
                    return false
                }

                return true
            }

        }

        viewBinding.webView.loadUrl(URL)
    }

I'm using Android webView to show a html page, I have a share option on my page.
when webpage load onto Android WebView and tapping on the share button nothing happening, How can i show the Share Sheet in android web view

     viewBinding.webView.apply {
    
                setBackgroundResource(R.drawable.window_bg);
                setBackgroundColor(android.graphics.Color.TRANSPARENT)
    
                settings.apply {
                    javaScriptEnabled = true
                    builtInZoomControls = false
                    displayZoomControls = false
                    allowFileAccess = true
                    allowContentAccess = true
                    domStorageEnabled = true
        
                }
            }

  viewBinding.webView.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                viewBinding.webView.visibility = View.GONE
                viewBinding.progressBar.visibility = View.GONE
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                viewBinding.progressBar.visibility = View.GONE
                viewBinding.webView.visibility = View.VISIBLE
            }

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


            override fun shouldOverrideUrlLoading(webView: WebView, url: String): Boolean {
                if (url.startsWith("http"))
                    return false //open web links as usual

                if (url.startsWith("intent:")) {
                    val intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME)
                    val data = intent.data
                    data?.let {
                        try {
                            val scheme = data.scheme
                            if (FACEBOOK_APP_SCHEMA == scheme) {
                                startActivity(intent)
                                return true
                            }
                        } catch (e: ActivityNotFoundException) {
                            val marketIntent = Intent(Intent.ACTION_VIEW).setData(
                                Uri.parse("$PLAY_STORE_URL${intent.getPackage()}")
                            )
                            if (marketIntent.resolveActivity(packageManager) != null) {
                                [email protected](marketIntent)
                                return true
                            }
                        }
                    }

                }
                return false
            }
        }

        viewBinding.webView.webChromeClient = object : WebChromeClient() {
            override fun onPermissionRequest(request: PermissionRequest?) {
                request?.grant(request.resources);
            }

           
            override fun onShowFileChooser(
                webView: WebView?,
                filePathCallback: ValueCallback<Array<Uri>>?,
                fileChooserParams: FileChooserParams?
            ): Boolean {
                uploadMessage?.onReceiveValue(null)
                uploadMessage = null

                uploadMessage = filePathCallback

                val intent = fileChooserParams!!.createIntent()
                try {
                    startActivityForResult(intent, REQUEST_SELECT_FILE)
                } catch (e: ActivityNotFoundException) {
                    uploadMessage = null
                    Toast.makeText(
                        applicationContext,
                        "Cannot Open File Chooser",
                        Toast.LENGTH_LONG
                    ).show()
                    return false
                }

                return true
            }

        }

        viewBinding.webView.loadUrl(URL)
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文