前言:本來android 自4.0之后已經(jīng)優(yōu)化了內核,改用了了chrome內核,兼容了大部分網(wǎng)頁,這不第三方使用了一個直播,強烈要求使用騰訊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內核加載失敗肝箱,會自動切換到系統(tǒng)內核叹谁。
MyLog.d("x5WebApp", " onViewInitFinished is " + arg0);
}
@Override
public void onCoreInitFinished() {
}
};
//x5內核初始化接口
QbSdk.initX5Environment(getApplicationContext(), cb);
}
然后寫個webview繼承騰訊X5的webview,記得千萬別倒錯包哦!
代碼如下:
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: zhang_quan_888@163.com
Time: 2018/12/4
-
Description:
*/
public class X5WebView extends WebView {
private WebViewClient client = new WebViewClient() {
// 防止加載網(wǎng)頁時調起系統(tǒng)瀏覽器
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一樣.
如有其他疑問,請聯(lián)系:zhang_quan_888@163.com,歡迎指正!
原文地址:https://blog.csdn.net/qq_38508087/article/details/84796831