本文共 3662 字,大约阅读时间需要 12 分钟。
腾讯x5 WebView 是基于 TBS(腾讯浏览器服务)框架开发的,适用于需要原生浏览器功能的 Android 应用开发。通过 WebView 可以加载网页、视频、图片等资源,并支持 JavaScript 等功能。
本文将介绍如何配置腾讯x5 WebView,避免默认浏览器跳转,并实现原生浏览器功能。
在调用 TBS 初始化、创建 WebView 之前,需配置相关设置:
// 在应用的onCreate方法中val map = mapOf( TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER to true, TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE to true)QbSdk.initTbsSettings(map)
创建自定义的 X5WebView 类,继承自 WebView,并设置合适的 WebViewClient:
package com.anguomob.video.viewimport android.annotation.SuppressLintimport android.content.Contextimport android.util.AttributeSetimport com.tencent.smtt.sdk.WebSettingsimport com.tencent.smtt.sdk.WebViewimport com.tencent.smtt.sdk.WebViewClientclass X5WebView @SuppressLint("SetJavaScriptEnabled") constructor( arg0: Context?, arg1: AttributeSet?) : WebView(arg0, arg1) { private val client: WebViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean { view.loadUrl(url) return true } } private fun initWebViewSettings() { val webSetting: WebSettings = this.getSettings() webSetting.setJavaScriptEnabled(true) webSetting.setJavaScriptCanOpenWindowsAutomatically(true) webSetting.setAllowFileAccess(true) webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS) webSetting.setSupportZoom(true) webSetting.setBuiltInZoomControls(true) webSetting.setUseWideViewPort(true) webSetting.setSupportMultipleWindows(true) webSetting.setLoadWithOverviewMode(true) webSetting.setAppCacheEnabled(true) webSetting.setDatabaseEnabled(true) webSetting.setDomStorageEnabled(true) webSetting.setGeolocationEnabled(true) webSetting.setAppCacheMaxSize(Long.MAX_VALUE) webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND) webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH) webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE) } init { this.setWebViewClient(client) initWebViewSettings() this.view?.setClickable(true) }} 确保在自定义 WebView 中设置了正确的 WebViewClient:
// 在 X5WebView 类中this.setWebViewClient(client)
在 Activity 中使用 X5WebView:
package com.anguomob.video.activityimport android.os.Bundleimport android.view.KeyEventimport android.view.Viewimport androidx.appcompat.widget.Toolbarimport com.anguomob.video.Rimport com.tencent.smtt.sdk.WebSettingsimport com.tencent.smtt.sdk.WebViewimport com.tencent.smtt.sdk.WebViewClientimport com.yilan.sdk.common.ui.BaseActivityclass WebViewX5Acitivity : BaseActivity() { lateinit var mWebView: WebView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_x5_webview) mWebView = findViewById(R.id.forum_context) mWebView.loadUrl("https://www.anguomob.com") // 初始化TBS设置 val map = mapOf( TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER to true, TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE to true ) QbSdk.initTbsSettings(map) } override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { return if (keyCode == KeyEvent.KEYCODE_BACK) { if (mWebView.canGoBack()) { mWebView.goBack() true } else { finish() true } } else { false } }} 通过以上方法,可以实现原生浏览器功能,避免默认浏览器跳转。配置合适的 WebViewClient 和 WebSettings 是关键,确保应用在不同设备和网络环境下的稳定性和性能。
转载地址:http://orsr.baihongyu.com/