博客
关于我
解决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/

你可能感兴趣的文章
Postman接口传参案例
查看>>
postman接口功能测试
查看>>
Postman接口测试
查看>>
Postman接口测试
查看>>
Postman接口测试/接口自动化实战教程
查看>>
Postman接口测试之POST、GET请求方法
查看>>
Postman接口测试之Post、Get请求方法
查看>>
Postman接口测试之:添加Cookie伪造请求
查看>>
Postman接口测试企业级实战
查看>>
PostMan接口测试实战
查看>>
Postman接口测试实战讲解
查看>>
Postman接口测试工具安装部署与快速入门
查看>>
postman接口测试常见问题
查看>>
postman接口测试,1个参数有好几个值的时候如何测试比较简单快速?
查看>>
Postman接口自动化测试之——批量参数化(参数文件)
查看>>
Postman接口自动化测试之——批量执行(集合操作)
查看>>
Postman接口自动化测试之——批量执行(集合操作)
查看>>
Postman断言与依赖接口测试详解!
查看>>
Postman环境变量以及设置token全局变量!
查看>>
postman的使用
查看>>