webview設定不彈出許可權請求彈窗
阿新 • • 發佈:2021-07-21
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了,有什麼出入的話,姐妹萌對比官方文件參考一下