Android集成腾讯X5浏览器内核库
生活随笔
收集整理的這篇文章主要介紹了
Android集成腾讯X5浏览器内核库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android集成騰訊X5瀏覽器內核庫
- 一、相關配置
- 1. 相關地址
- 2.引入SDK
- 3. AndroidManifest配置
- 二、Application中初始化內核
- 三、代碼實現
- 1. 自定義帶ProgressBar的WebView
- 2. activity_main.xml
- 3. MainActivity
一、相關配置
1. 相關地址
https://x5.tencent.com/2.引入SDK
在build.gradle中dependencies引入sdk
api 'com.tencent.tbs.tbssdk:sdk:43903'build.gradle中的andorid->defaultConfig中
ndk {abiFilters "armeabi", "armeabi-v7a", "x86", "mips"}3. AndroidManifest配置
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.READ_SETTINGS" /><uses-permission android:name="android.permission.WRITE_SETTINGS" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />二、Application中初始化內核
class APPAplication : Application() {override fun onCreate() {super.onCreate()//搜集本地tbs內核信息并上報服務器,服務器返回結果決定使用哪個內核。val cb: QbSdk.PreInitCallback =object : QbSdk.PreInitCallback {override fun onViewInitFinished(arg0: Boolean) {//x5內核初始化完成的回調,為true表示x5內核加載成功,否則表示x5內核加載失敗,會自動切換到系統內核。}override fun onCoreInitFinished() {}}//x5內核初始化接口QbSdk.initX5Environment(applicationContext, cb)} }三、代碼實現
1. 自定義帶ProgressBar的WebView
import android.content.Context import android.util.AttributeSet import android.view.View import android.widget.AbsoluteLayout import android.widget.ProgressBar import com.huang.myapplication.R import com.tencent.smtt.export.external.interfaces.SslError import com.tencent.smtt.export.external.interfaces.SslErrorHandler import com.tencent.smtt.sdk.WebChromeClient import com.tencent.smtt.sdk.WebSettings import com.tencent.smtt.sdk.WebView import com.tencent.smtt.sdk.WebViewClientinternal class ProgressWebview : WebView {//進度條private var progressbar: ProgressBar? = null//進度條的高度,默認5pxprivate val progressHeight = 5constructor(context: Context) : super(context) {initView(context)}constructor(context: Context, attributeSet: AttributeSet?) : super(context,attributeSet) {initView(context)}private fun initView(context: Context) {//開啟js腳本支持settings.javaScriptEnabled = true//創建進度條progressbar = ProgressBar(context, null,android.R.attr.progressBarStyleHorizontal)//設置加載進度條的高度progressbar!!.layoutParams = AbsoluteLayout.LayoutParams(LayoutParams.MATCH_PARENT,progressHeight,0,0)var drawable = context.getResources().getDrawable(R.drawable.progress_bar_states);progressbar!!.setProgressDrawable(drawable);//添加進度到WebViewaddView(progressbar)//適配手機大小settings.useWideViewPort = truesettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNSsettings.loadWithOverviewMode = truesettings.setSupportZoom(true)settings.builtInZoomControls = truesettings.displayZoomControls = falsewebChromeClient = WVChromeClient()webViewClient = WVClient()}//進度顯示private inner class WVChromeClient : WebChromeClient() {override fun onProgressChanged(view: WebView,newProgress: Int) {if (newProgress == 100) {progressbar!!.visibility = View.GONE} else {if (progressbar!!.visibility == View.GONE) progressbar!!.visibility = View.VISIBLEprogressbar!!.progress = newProgress}if (mListener != null) {mListener!!.onProgressChange(view, newProgress)}super.onProgressChanged(view, newProgress)}}private inner class WVClient : WebViewClient() {override fun shouldOverrideUrlLoading(view: WebView,url: String): Boolean {//在當前Activity打開view.loadUrl(url)return true}override fun onReceivedSslError(view: WebView,handler: SslErrorHandler,error: SslError) {//https忽略證書問題handler.proceed()}override fun onPageFinished(view: WebView,url: String) {progressbar!!.visibility = View.GONEif (mListener != null) {mListener!!.onPageFinish(view)}super.onPageFinished(view, url)}}private var mListener: onWebViewListener? = nullfun setOnWebViewListener(listener: onWebViewListener?) {mListener = listener}//進度回調接口interface onWebViewListener {fun onProgressChange(view: WebView?,newProgress: Int)fun onPageFinish(view: WebView?)} }2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.huang.myapplication.ui.ProgressWebviewandroid:id="@+id/progress_web_view"android:layout_width="match_parent"android:layout_height="match_parent" /></androidx.constraintlayout.widget.ConstraintLayout>3. MainActivity
class MainActivity : AppCompatActivity() {val url = "https://debugtbs.qq.com/"override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val webSetting: WebSettings = progress_web_view.getSettings()webSetting.allowFileAccess = truewebSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNSwebSetting.setSupportZoom(true)webSetting.builtInZoomControls = truewebSetting.useWideViewPort = truewebSetting.setSupportMultipleWindows(false)webSetting.setAppCacheEnabled(true)webSetting.domStorageEnabled = truewebSetting.javaScriptEnabled = truewebSetting.setGeolocationEnabled(true)webSetting.setAppCacheMaxSize(Long.MAX_VALUE)webSetting.setAppCachePath(getDir("appcache", 0).path)webSetting.databasePath = getDir("databases", 0).pathwebSetting.setGeolocationDatabasePath(getDir("geolocation", 0).path)webSetting.pluginState = WebSettings.PluginState.ON_DEMANDprogress_web_view.loadUrl(url)CookieSyncManager.createInstance(this)CookieSyncManager.getInstance().sync()progress_web_view.setOnWebViewListener(object : onWebViewListener {override fun onProgressChange(view: WebView?, newProgress: Int) {Log.d("app","onProgressChange=="+newProgress)}override fun onPageFinish(view: WebView?) {Log.d("app","onPageFinish==")}})}override fun onResume() {super.onResume()progress_web_view.onResume()}override fun onPause() {super.onPause()progress_web_view.onPause()} }總結
以上是生活随笔為你收集整理的Android集成腾讯X5浏览器内核库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python控制已经打开的浏览器_使用p
- 下一篇: 不同浏览器的userAgent