赞
踩
解决办法是使用腾讯X5内核
一、在 build.gradle 引入 dependencies
- //腾讯内核WebView
- api 'com.tencent.tbs.tbssdk:sdk:43903'
二、在 build.gradle 设置 ndk
- ndk {
- // 设置支持的SO库架构
- abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a', "mips"
- }
三、在 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" />
四、在 AndroidManifest.xml 设置 activity 配置
- <!--这个是实现WebView的activity-->
- <activity
- android:name=".ui.MainActivity"
- android:configChanges="orientation|screenSize|keyboardHidden"
- android:windowSoftInputMode="stateHidden|adjustResize"
- android:hardwareAccelerated="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.LAUNCHER" /
- </intent-filter>
- </activity>
-
-
- <!--视频播放Activity-->
- <activity
- android:name="com.tencent.smtt.sdk.VideoActivity"
- android:configChanges="orientation|screenSize|keyboardHidden"
- android:exported="false"
- android:launchMode="singleTask"
- android:alwaysRetainTaskState="true">
- <intent-filter>
- <action android:name="com.tencent.smtt.tbs.video.PLAY" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>

五、在 Application 里初始化
- //在Application中加载X5内核
- private void initX5(){
- 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);
-
- QbSdk.setDownloadWithoutWifi(true);
- Log.d("LOG_TAG", "开始加载内核");
- QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
- @Override
- public void onCoreInitFinished() {
-
- }
-
- @Override
- public void onViewInitFinished(boolean b) {
- if (b){
- Log.d("LOG_TAG", "内核加载成功");
- }else{
- Log.d("LOG_TAG", "内核加载失败");
- }
- }
- });
- QbSdk.setTbsListener(new TbsListener() {
- @Override
- public void onDownloadFinish(int i) {
- Log.d("LOG_TAG", "下载完成 progress = " + i);
- }
-
- @Override
- public void onInstallFinish(int i) {
- Log.d("LOG_TAG", "正在安装内核 progress = " + i);
- }
-
- @Override
- public void onDownloadProgress(int i) {
- Log.d("LOG_TAG", "已经下载 progress = " + i);
- }
- });
- }

六、更改使用腾讯X5内核 WebView
- <!--将 WebView -->
- <WebView
- android:id="@+id/webView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"/>
-
-
- <!--改为 com.tencent.smtt.sdk.WebView-->
- <com.tencent.smtt.sdk.WebView
- android:id="@+id/webView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"/>
七、将代码中相关的 import android.webkit.* 改为 com.tencent.smtt.*
具体根据自己代码改
- /*import android.webkit.JsResult;
- import android.webkit.URLUtil;
- import android.webkit.ValueCallback;
- import android.webkit.WebChromeClient;
- import android.webkit.WebResourceError;
- import android.webkit.WebResourceRequest;
- import android.webkit.WebSettings;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;*/
-
-
- import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
- import com.tencent.smtt.export.external.interfaces.JsResult;
- import com.tencent.smtt.export.external.interfaces.WebResourceError;
- import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
- import com.tencent.smtt.sdk.URLUtil;
- import com.tencent.smtt.sdk.ValueCallback;
- import com.tencent.smtt.sdk.WebChromeClient;
- import com.tencent.smtt.sdk.WebSettings;
- import com.tencent.smtt.sdk.WebView;

八、添加一些配置
下面这些需要哪些根据需要自己配置
- //支持设置
- WebSettings webSettings = webView.getSettings();
- //允许使用js
- webSettings.setJavaScriptEnabled(true);
- //设置DOM储存
- webSettings.setDomStorageEnabled(true);
- webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
- //WebView屏幕自适应
- webSettings.setUseWideViewPort(true);
- //WebView屏幕自适应
- webSettings.setLoadWithOverviewMode(true);
- //支持缩放
- webSettings.setSupportZoom(true);
- webSettings.setBuiltInZoomControls(true);
- //支持内容重新布局
- webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
- //多窗口
- webSettings.supportMultipleWindows();
- //设置可以访问文件
- webSettings.setAllowFileAccess(true);
- //当webview调用requestFocus时为webview设置节点
- webSettings.setNeedInitialFocus(true);
- //支持通过JS打开新窗口
- webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
- //支持自动加载图片
- webSettings.setLoadsImagesAutomatically(true);
- //关闭硬件加速
- //webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
- //webView.setLayerType();
- //系统选择一种支持半透明(许多alpha位)的格式
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- //硬件加速渲染
- webView.setDrawingCacheEnabled(true);
- webView.setVerticalScrollBarEnabled(false); // 取消Vertical ScrollBar显示
- webView.setHorizontalScrollBarEnabled(false); // 取消Horizontal ScrollBar显示
- //webView.requestFocus();
- * if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
- }
- //webView.setFocusable(false);
- webSettings.setDefaultTextEncodingName("GBK");//设置文本编码(根据页面要求设置: utf-8)*/
- webView.setWebViewClient(webViewClient);
- webView.setWebChromeClient(webChromeClient);
- //保存图片
- webView.setOnLongClickListener(onLongClickListener);
- webView.loadUrl(Constants.URL);

- //这两个没有设置可能会导致视频不能全屏
- //系统选择一种支持半透明(许多alpha位)的格式
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- //硬件加速渲染
- webView.setDrawingCacheEnabled(true);
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。