版权声明:版权为博主所有,如需转载,请注明来源 https://blog.csdn.net/qq_38508087/article/details/84796831
前言:本来android 自4.0之后已经优化了内核,改用了了chrome内核,兼容了大部分网页,这不第三方使用了一个直播,强烈要求使用腾讯X5的内核,说是兼容更好一点,既然这样,我就集成试试,下面教你快速集成:
1.腾讯浏览服务官方:https://x5.tencent.com/tbs/sdk.html
然后导入:
这个官方demo都有,放在自己的项目下,然后再BaseApplication里面初始化一下:
附上代码:
private void initX5Web() { QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() { @Override public void onViewInitFinished(boolean arg0) { //x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。 MyLog.d("x5WebApp", " onViewInitFinished is " + arg0); } @Override public void onCoreInitFinished() { } }; //x5内核初始化接口 QbSdk.initX5Environment(getApplicationContext(), cb); }
然后写个webview继承腾讯X5的webview,记得千万别倒错包哦!
代码如下:
扫描二维码关注公众号,回复:
5504111 查看本文章
package com.hsz88.zbx.x5webview; import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import com.hsz88.zbx.base.BaseApplication; import com.hsz88.zbx.constant.StaticConfig; import com.hsz88.zbx.webview.DefaultJsObject; import com.tencent.smtt.sdk.WebSettings; import com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm; import com.tencent.smtt.sdk.WebView; import com.tencent.smtt.sdk.WebViewClient; /** * Author: KuenCheung * Email: [email protected] * Time: 2018/12/4 * Description: */ public class X5WebView extends WebView { private WebViewClient client = new WebViewClient() { // 防止加载网页时调起系统浏览器 public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }; public X5WebView(Context arg0) { super(arg0); setBackgroundColor(85621); } @SuppressLint("SetJavaScriptEnabled") @Deprecated public X5WebView(Context arg0, AttributeSet arg1) { super(arg0, arg1); this.setWebViewClient(client); // this.setWebChromeClient(chromeClient); // WebStorage webStorage = WebStorage.getInstance(); initWebViewSettings(); this.getView().setClickable(true); } @SuppressLint("SetJavaScriptEnabled") @Deprecated private void initWebViewSettings() { 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.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.setRenderPriority(WebSettings.RenderPriority.HIGH); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); //设置默认的JS Object addJavascriptInterface(new DefaultJsObject(BaseApplication.mContext), StaticConfig.JS_OBJECT); } }
然后,在自己的xml俩面进行使用,具体方法和webview一样,如下:
<com.hsz88.zbx.x5webview.X5WebView android:id="@+id/default_web" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" />
其他操作和webview一样.
如有其他疑问,请联系:[email protected],欢迎指正!