初始化X5服务
//在application中初始化 @Override public void onCreate() { super.onCreate();
//搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。 QbSdk.PreInitCallback callback = new QbSdk.PreInitCallback() { @Override public void onViewInitFinished(boolean arg) { //x5內核初始化完成的回调, // true表示x5内核加载成功, // false表示x5内核加载失败,会自动切换到系统内核。 }
@Override public void onCoreInitFinished() { } };
QbSdk.initX5Environment(getApplicationContext(), callback); }
在布局中添加 WebView
<com.tencent.smtt.sdk.WebView android:id="@+id/webview" android:layout_width=“match_parent” android:layout_height=“match_parent” />
初始化 Webview
public class MainActivity extends AppCompatActivity { private ProgressBar progressBar; private WebView webView;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = findViewById(R.id.progressbar); webView = findViewById(R.id.webview); initWebView(); }
private void initWebView() { WebSettings settings = webView.getSettings(); //和系统webview一样 settings.setJavaScriptEnabled(true); //支持Javascript 与js交互 settings.setJavaScriptCanOpenWindowsAutomatically(true);//支持通过JS打开新窗口 settings.setAllowFileAccess(true); //设置可以访问文件 settings.setSupportZoom(true); //支持缩放 settings.setBuiltInZoomControls(true); //设置内置的缩放控件 settings.setUseWideViewPort(true); //自适应屏幕 settings.setSupportMultipleWindows(true); //多窗口 settings.setDefaultTextEncodingName(“utf-8”); //设置编码格式 settings.setAppCacheEnabled(true); settings.setDomStorageEnabled(true); settings.setAppCacheMaxSize(Long.MAX_VALUE); settings.setCacheMode(WebSettings.LOAD_NO_CACHE); //缓存模式 webView.setWebViewClient(new WebViewClient() {
@Override public void onPageStarted(WebView webView, String s, Bitmap bitmap) { super.onPageStarted(webView, s, bitmap); }
@Override public void onPageFinished(WebView webView, String s) { super.onPageFinished(webView, s); }
@Override public boolean shou
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
ldOverrideUrlLoading(WebView webView, String url) { webView.loadUrl(url); return true; }
@Override public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) { // super.onReceivedSslError(webView, sslErrorHandler, sslError); sslErrorHandler.proceed();//忽略SSL证书错误 } });
webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView webView, String s, String s1, JsResult jsResult) { return super.onJsAlert(webView, s, s1, jsResult); }
@Override public void onReceivedTitle(WebView webView, String s) { super.onReceivedTitle(webView, s); }
@Override public void onProgressChanged(WebView webView, int progress) { super.onProgressChanged(webView, progress); if(…){ … progressBar.setProgress(progress); //设置进度条 … } else { … } } } }); webView.loadUrl(“http://www.baidu.com”); // webView.reload(); 刷新页面 }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && webView != null && webView.canGoBack()) { webView.goBack(); return true;
|