简介 在 Android 开发项目中,经常会用到 WEBview 。而 WebView 是出了名的坑,各种 Bug。腾讯 TBS 浏览服务面向应用开发商和广大开发者,提供浏览增强,内容框架,广告体系,
在 Android 开发项目中,经常会用到 WEBview 。而 WebView 是出了名的坑,各种 Bug。腾讯 TBS 浏览服务面向应用开发商和广大开发者,提供浏览增强,内容框架,广告体系,H5游戏分发,大数据等服务,能够帮助应用开发商大幅改善应用体验,有效提升开发,运营,商业化的效率。
官网地址: https://x5.tencent.com/
其中,X5云端服务包括云加速、云安全、云转换三大功能。云加速是通过首屏加速、智能路由、图片压缩等技术保障网页浏览的快、省、悦;云安全是通过代理加密、URL安全检测、js黑名单管理、负载监控等防范网页被劫持插入广告、保障网页浏览的安全稳定;云转换是使用URL聚合+XPATH的解决方案,解决移动阅读难题。
方式一:jar包方式集成(推荐)
您可将官网下载的jar包复制到您的App的libs目录,并且通过Add As Library的方式集成TBS SDK。
方式二:自动集成
使用 MavenCentral 仓库
在项目级别(通常是根目录下)的 build.gradle 中添加:
repositories { Google() // 增加这行 mavenCentral()}
在应用级别(通常是 app 模块下)的 build.gradle 中添加依赖:
dependencies { ... // 增加这行 implementation 'com.tencent.tbs:tbssdk:44286'}
在 AndroidManifest.xml
里权限声明
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.GET_TASKS"/>
QbSdk.PreInitCallback callback = new QbSdk.PreInitCallback() { @Override public void onViewInitFinished(boolean arg) { //x5內核初始化完成的回调, // true表示x5内核加载成功, // false表示x5内核加载失败,会自动切换到系统内核。 Log.i("kevin", "X5内核是否成功加载= " + arg); } @Override public void onCoreInitFinished() { } }; QbSdk.setTbsListener(new TbsListener() { @Override public void onDownloadFinish(int i) { Log.i("kevin", "onDownloadFinish: " + i); } @Override public void onInstallFinish(int i) { Log.i("kevin", "onInstallFinish: " + i); } @Override public void onDownloadProgress(int i) { Log.i("kevin", "onInstallFinish: " + i); } }); QbSdk.initX5Environment(getApplicationContext(), callback);
private void initWebSettings() { WebSettings webSetting = this.getSettings(); webSetting.setjavascriptEnabled(true); webSetting.setJavaScriptCanOpenwindowsAutomatically(true); webSetting.setAllowFileAccess(true); webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); webSetting.setSupportZoom(true); webSetting.setBuiltInZoomControls(true); webSetting.setUseWideViewPort(true); webSetting.setSupportMultipleWindows(true); webSetting.setAppCacheEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);}
如果在项目中使用,一般来说最好是封装多一层。
public class ProgressWebView extends WebView { //进度条 private ProgressBar progressbar; //进度条的高度,默认10px private int progressheight = 10; public ProgressWebView(Context context) { this(context,null); } public ProgressWebView(Context context, AttributeSet attributeSet) { super(context, attributeSet); initView(context); } private void initView(Context context) { //创建进度条 progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal); //设置加载进度条的高度 progressbar.setLayoutParams(new AbsoluteLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, progressHeight, 0, 0)); Drawable drawable = context.getResources().getDrawable(R.drawable.progress_bar_states); progressbar.setProgressDrawable(drawable); //添加进度到WebView addView(progressbar); //初始化 WebSettings initWebSettings(); setWebChromeClient(new WVChromeClient()); setWebViewClient(new WVClient()); } private void initWebSettings() { WebSettings webSetting = 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.setAppCacheEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); } //进度显示 private class WVChromeClient extends WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) { progressbar.setVisibility(GONE); } else { if (progressbar.getVisibility() == GONE) { progressbar.setVisibility(VISIBLE); } progressbar.setProgress(newProgress); } if (mListener != null) { mListener.onProgressChange(view, newProgress); } super.onProgressChanged(view, newProgress); } } private class WVClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //在当前Activity打开 view.loadUrl(url); return true; } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { //https忽略证书问题 handler.proceed(); } @Override public void onPageFinished(WebView view, String url) { progressbar.setVisibility(GONE); if (mListener != null) { mListener.onPageFinish(view); } super.onPageFinished(view, url); } } private onWebViewListener mListener; public void setOnWebViewListener(onWebViewListener listener) { this.mListener = listener; } //进度回调接口 public interface onWebViewListener { void onProgressChange(WebView view, int newProgress); void onPageFinish(WebView view); }}
来源地址:https://blog.csdn.net/duoduo_11011/article/details/130989566
--结束END--
本文标题: 【Android -- 开源库】腾讯 TBS 浏览器 SDK 接入
本文链接: https://lsjlt.com/news/418459.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0