X5WebView前端开发时如何集成
程序员文章站
2022-04-19 14:59:00
官网文档:https://x5.tencent.com/docs/index.html参考博客:https://blog.csdn.net/weixin_38025168/article/details/107044399一、gradleimplementation 'com.tencent.tbs.tbssdk:sdk:43903'二、权限1、permission
官网文档:https://x5.tencent.com/docs/index.html
参考博客:https://blog.csdn.net/weixin_38025168/article/details/107044399
一、gradle
implementation 'com.tencent.tbs.tbssdk:sdk:43903'
二、权限
1、permission
<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" />
2、允许http请求
application增加android:usesCleartextTraffic="true"属性
3、provider
(1)res-xml下新建xxx.xml,内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="external_storage_root"
path="."/>
</paths>
</resources>
(2)manifest增加配置:
<provider
android:name="com.tencent.smtt.utils.FileProvider"
android:authorities="包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/xml文件名" />
</provider>
三、初始化
1、application类的oncreate函数中:
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);
QbSdk.setTbsListener(new TbsListener() {
@Override
public void onDownloadFinish(int i) {
Log.d("app", "onDownloadFinish -->下载X5内核完成:" + i);
}
@Override
public void onInstallFinish(int i) {
Log.d("app", "onInstallFinish -->安装X5内核进度:" + i);
}
@Override
public void onDownloadProgress(int i) {
Log.d("app", "onDownloadProgress -->下载X5内核进度:" + i);
}
});
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
@Override
public void onViewInitFinished(boolean arg0) {
//x5內核初始化完成的回调,true表x5内核加载成功,否则表加载失败,会自动切换到系统内核。
Log.d("app", " 内核加载 " + arg0);
}
@Override
public void onCoreInitFinished() {
}
};
//x5内核初始化接口
QbSdk.initX5Environment(getApplicationContext(), cb);
2、manifest中添加服务
<service
android:name="com.tencent.smtt.export.external.DexClassLoaderProviderService"
android:label="dexopt"
android:process=":dexopt" >
</service>
四、自定义X5WebView
//WebView、WebSettings、WebViewClient要引用com.tencent.smtt.sdk.xxx包
public class X5WebView extends WebView {
private WebViewClient client = new WebViewClient() {
/**
* 防止加载网页时调起系统浏览器
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
};
@SuppressLint("SetJavaScriptEnabled")
public X5WebView(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
this.setWebViewClient(client);
initWebViewSettings();
this.getView().setClickable(true);
}
private void initWebViewSettings() {
WebSettings mWebSettings = this.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
mWebSettings.setAllowFileAccess(true);
mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
mWebSettings.setSupportZoom(true);
mWebSettings.setBuiltInZoomControls(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setSupportMultipleWindows(true);
mWebSettings.setAppCacheEnabled(true);
mWebSettings.setDomStorageEnabled(true);
mWebSettings.setGeolocationEnabled(true);
mWebSettings.setAppCacheMaxSize(Long.MAX_VALUE);
mWebSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
}
public X5WebView(Context arg0) {
super(arg0);
setBackgroundColor(85621);
}
}
本文地址:https://blog.csdn.net/victonia600/article/details/107305243
上一篇: Android 版本适配 6~11