博客
关于我
解决Android中WebView跳转到浏览器的问题
阅读量:354 次
发布时间:2019-03-04

本文共 3662 字,大约阅读时间需要 12 分钟。

使用腾讯x5 WebView实现原生浏览器

一、技术背景

腾讯x5 WebView 是基于 TBS(腾讯浏览器服务)框架开发的,适用于需要原生浏览器功能的 Android 应用开发。通过 WebView 可以加载网页、视频、图片等资源,并支持 JavaScript 等功能。

二、核心解决方案

本文将介绍如何配置腾讯x5 WebView,避免默认浏览器跳转,并实现原生浏览器功能。

1. 配置TBS初始化

在调用 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)

2. 自定义WebView

创建自定义的 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)    }}

3. 解决默认浏览器问题

确保在自定义 WebView 中设置了正确的 WebViewClient:

// 在 X5WebView 类中this.setWebViewClient(client)

4. 使用示例

在 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/

你可能感兴趣的文章
python3.5实现socket通讯(TCP)
查看>>
python3.6 ubuntu部署nginx、 uwsgi、 django
查看>>
Python3.6.3中,functools似乎不能用
查看>>
python3.6.5+cuda9+cudnn7.1+win10+tensorflow-gpu1.9.0下载配置
查看>>
python3.6使用pickle序列化class
查看>>
python3.6入门到高阶(全栈) day027 网络通信协议 osi七层模型
查看>>
Python3.6属性错误:模块‘;异步没有属性‘;运行‘;
查看>>
python3.6版本中的python-weka-wrapper安装失败,错误代码为%1
查看>>
Python3.7安装cv2踩过的坑
查看>>
python写txt文件
查看>>
python关于sorted排序函数详细解析 附实战代码(全)
查看>>
python3.8调用m文件,调用matlab
查看>>
python3.9新特性_Python 3.9 新特性速览
查看>>
Python3.x:遍历select下拉框获取value值
查看>>
python360百科_11月编程排行榜:Python超过Java成第二
查看>>
Python关于Paginator分页函数详细解析 附实战代码
查看>>
python3、ipython3、setup-tools、pip等环境搭建详细总结
查看>>
python3下使用cv2.imwrite存储带有中文路径图片或者绝对路径图片
查看>>
python3与django中@property详解
查看>>
python3中如何表示无穷大
查看>>