添加依赖
dependencies {
api 'com.yanzhenjie:permission:2.0.3'
api 'com.tencent.tbs.tbssdk:sdk:43939' // 腾讯X5WebView
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
在注册清单中添加service
<service
android:name="com.tencent.smtt.export.external.DexClassLoaderProviderService"
android:label="dexopt"
android:process=":dexopt" >
</service>
在application中初始化
import android.app.Application;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import java.util.HashMap;
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
initX5WebView();
}
/**
* 初始化x5WebView
*/
private void initX5WebView() {
// 搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
@Override
public void onViewInitFinished(boolean arg0) {
// x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
LogUtils.d("onViewInitFinished is "+arg0);
}
@Override
public void onCoreInitFinished() {
}
};
// x5内核初始化接口
QbSdk.initX5Environment(this, cb);
// 在调用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);
}
}
在activity中基础使用
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.tencent.smtt.sdk.QbSdk
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebView
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
AndPermission.with(this)
.runtime()
.permission(Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_PHONE_STATE)
.onGranted { }
.onDenied { }
.start()
// 加载进度
forum_context.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(p0: WebView?, p1: Int) {
super.onProgressChanged(p0, p1)
LogUtils.d(p1)
}
}
forum_context.settings.javaScriptEnabled = true // 开启js
forum_context.settingsExtension.setDisplayCutoutEnable(true) // 刘海屏适配
forum_context.loadUrl("https://www.baidu.com") // 加载url
}
override fun onDestroy() {
QbSdk.clearAllWebViewCache(this, true) // 清除缓存
super.onDestroy()
}
}
activity的xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.tencent.smtt.sdk.WebView
android:id="@+id/forum_context"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>