本人使用X5主要解决两个问题:
接入参考 腾讯TBS官网 ->快速接入
使用参考 Android SDK(完整版,含文件打开能力)
1 两种方式引入jar,推荐Gradle方式集成
implementation 'com.tencent.tbs.tbssdk:sdk:43903'
2 权限配置
在 AndroidManifest.xml增加如下权限:
3 混淆配置
-dontwarn dalvik.**
-dontwarn com.tencent.smtt.**
-keep class com.tencent.smtt.** {
*;
}
-keep class com.tencent.tbs.** {
*;
}
4 在application中初始化SDK。
private void initX5() {
// 在调用TBS初始化、创建WebView之前进行如下配置
HashMap map = new HashMap();
map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
QbSdk.initTbsSettings(map);
//搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
@Override
public void onViewInitFinished(boolean arg0) {
//x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
if (arg0) {
Log.e("ArticleSystem", "X5 内核加载成功");
} else {
Log.e("ArticleSystem", "X5 内核加载失败");
}
}
@Override
public void onCoreInitFinished() {
}
};
//x5内核初始化接口
QbSdk.initX5Environment(getApplicationContext(), cb);
}
5 在 AndroidManifest.xml中增加内核首次加载时优化Service声明:
6 自定义X5WebView
public class X5WebView extends WebView {
private WebViewClient client = new WebViewClient() {
/**
* 防止加载网页时调起系统浏览器
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
LogUtil.e("xs sdk = "+ Build.VERSION.SDK_INT);//23
//view.loadUrl(url);
if(url.startsWith("http://") || url.startsWith("https://")){
view.loadUrl(url);
}else {
if(view != null && view.getContext() != null){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
}
}
return true;
}
};
@SuppressLint("SetJavaScriptEnabled")
public X5WebView(Context context, AttributeSet arg1) {
super(context, arg1);
this.setWebViewClient(client);
// this.setWebChromeClient(chromeClient);
// WebStorage webStorage = WebStorage.getInstance();
initWebViewSettings();
this.getView().setClickable(true);
}
private void initWebViewSettings() {
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.setLoadWithOverviewMode(true);
webSetting.setAppCacheEnabled(true);
// webSetting.setDatabaseEnabled(true);
webSetting.setDomStorageEnabled(true);
webSetting.setGeolocationEnabled(true);
webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
// webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
//webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
webSetting.setPluginState(WebSettings.PluginState.ON);//视频播放
// webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSetting.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
// this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
// settings 的设计
}
public X5WebView(Context context) {
super(context);
setBackgroundColor(85621);
}
}
7 使用自定义的X5WebView加载网页Url 即可。
7.1 加载网页一般会加一个进度条 例如
进度条样式 progress_bar_states.xml
-
-
7.2 视频播放要启用硬件加速,如果只加载网页可以省略。
try {
if( ( Integer.parseInt(android.os.Build.VERSION.RELEASE)) >= 9 ){
if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
/**
* 启用硬件加速
*/
getActivity().getWindow().setFlags(android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
}
}
} catch (Exception e) {
}
或者 在AndroidManifest.xml Activity添加:
android:hardwareAccelerated="true"
7.3 Activity 或者 Fragment 中使用
/**
*
* @param vedioUrl 视频地址,网页地址
*/
private void startPlay(String vedioUrl) {
mX5WebView.loadUrl(vedioUrl);//如果只是网页地址,后面可省略
getWindow().setFormat(PixelFormat.TRANSLUCENT);
mX5WebView.getView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
mX5WebView.setWebChromeClient(new WebChromeClient());
}
private void initWebSettings() {
mX5WebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
//显示进度条
if (newProgress < 100) {
progressBar.setProgress(newProgress);
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
}
});
mX5WebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//点击事件
LogUtil.e("web setWebViewClient");
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
//加载错误
super.onReceivedError(view, request, error);
}
});
}
@Override
protected void onDestroy() {
if (mX5WebView != null) {
mX5WebView.destroy();
}
super.onDestroy();
}
7.4 返回键的处理
7.4.1 Activity
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mX5WebView.canGoBack()) {
mX5WebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
7.4.2.1 Fragment
@Override
public boolean onBackPressedSupport() {
LogUtil.e("fragment onBackPressedSupport");
if (mX5WebView.canGoBack()) {
mX5WebView.goBack();
return true;
}
return super.onBackPressedSupport();
}
7.4.2.2 Fragment 所在Activity,按需求是否需要添加
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mFragments[0].onBackPressedSupport()) {
LogUtil.e("main onBackPressedSupport");
return true;
}
}
return super.onKeyDown(keyCode, event);
}
参考
腾讯x5内核(TBS)集成使用详解(webView视频播放)