webview设置不弹出权限请求弹窗

Android使用WebView网页中要求获取麦克风权限,原生webView在setWebChromeClient()回调 onPermissionRequest 处理即可,不会弹窗(测试机系统9.0)

后来因为项目需要,更改为使用腾讯X5内核的WebView,权限请求时使用了原生中同样的方法,发现每次进入网页都会弹窗权限请求的弹框

 

 1 @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
 2             override fun onPermissionRequest(request: PermissionRequest?) {
 3                 try {
 4                     //授权网页获取权限
 5                     request?.grant(request.resources)
 6                     request?.resources?.forEach {
 7                         if (it == "android.webkit.resource.AUDIO_CAPTURE") {
 8                             RxPermissions(this@WebViewFragment).request(Manifest.permission.RECORD_AUDIO)
 9                                 .subscribe({ isAllow ->
10                                     if (!isAllow) {
11                                         ToastUtils.showShort("您拒绝了录音权限无法使用该功能,请在设置中打开")
12                                     }
13                                 }, {
14                                     it.printStackTrace()
15                                 })
16                         }
17                     }
18                 } catch (e: Exception) {
19                     e.printStackTrace()
20                 }
21             }

测试可用的解决办法是这样的(注意了嗷姐妹们!!重点在最后一个回调方法里):

webView.webChromeClientExtension = object : IX5WebChromeClientExtension {
            override fun getX5WebChromeClientInstance(): Any? {
                return null
            }

            override fun getVideoLoadingProgressView(): View? {
                return null
            }

            override fun onAllMetaDataFinished(p0: IX5WebViewExtension?,p1: HashMap<String, String>?) {}

            override fun onBackforwardFinished(p0: Int) {}

            override fun onHitTestResultForPluginFinished(p0: IX5WebViewExtension?,p1: IX5WebViewBase.HitTestResult?,p2: Bundle?) { }

            override fun onHitTestResultFinished(p0: IX5WebViewExtension?,p1: IX5WebViewBase.HitTestResult?) {}

            override fun onPromptScaleSaved(p0: IX5WebViewExtension?) { }

            override fun onPromptNotScalable(p0: IX5WebViewExtension?) {}

            override fun onAddFavorite(p0: IX5WebViewExtension?,p1: String?,p2: String?,p3: JsResult?): Boolean {
                return false
            }

            override fun onPrepareX5ReadPageDataFinished(p0: IX5WebViewExtension?,p1: HashMap<String, String>?) {}

            override fun onSavePassword(p0: String?,p1: String?,p2: String?,p3: Boolean,p4: Message?): Boolean {
                return false
            }

            override fun onSavePassword(p0: ValueCallback<String>?,p1: String?,p2: String?,p3: String?,p4: String?,p5: String?,p6: Boolean): Boolean {
                return false
            }

            override fun onX5ReadModeAvailableChecked(p0: HashMap<String, String>?) { }

            override fun addFlashView(p0: View?, p1: ViewGroup.LayoutParams?) { }

            override fun h5videoRequestFullScreen(p0: String?) {}

            override fun h5videoExitFullScreen(p0: String?) { }

            override fun requestFullScreenFlash() {}

            override fun exitFullScreenFlash() {}

            override fun jsRequestFullScreen() {}

            override fun jsExitFullScreen() {}

            override fun acquireWakeLock() {}

            override fun releaseWakeLock() {}

            override fun getApplicationContex(): Context? {
                return null
            }

            override fun onPageNotResponding(p0: Runnable?): Boolean {
                return false
            }

            override fun onMiscCallBack(p0: String?, p1: Bundle?): Any? {
                return null
            }

            override fun openFileChooser(p0: ValueCallback<Array<Uri>>?, p1: String?, p2: String?) { }

            override fun onPrintPage() { }

            override fun onColorModeChanged(p0: Long) {}

            override fun onPermissionRequest(s: String?,p1: Long,mediaAccessPermissionsCallback: MediaAccessPermissionsCallback?): Boolean {
                mediaAccessPermissionsCallback?.invoke(s,MediaAccessPermissionsCallback.ALLOW_AUDIO_CAPTURE,true)
                return true
            }

        }

以上,就OKK了,有什么出入的话,姐妹萌对比官方文档参考一下

 

补充:有bug遇到X5默认开启失败,还有一个原因可能是app本身未开启录音权限,需要本身有权限的前提下才能请求到网页中的权限。

 

posted @ 2021-07-21 18:10  Sharley  阅读(2657)  评论(0编辑  收藏  举报