使用webView中遇到的坑。一些属性设置,和替换成企鹅的X5内核

【使用webView中遇到的坑。一些属性设置,和替换成企鹅的X5内核】吐槽原生WebView,不友好。
先贴代码吧
activity_main.xml:


MainActivity.java
package com.example.www.test; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.KeyEvent; import android.view.Window; import android.view.WindowManager; import android.widget.Toast; import com.tencent.smtt.sdk.QbSdk; import com.tencent.smtt.sdk.WebSettings; import com.tencent.smtt.sdk.WebView; import com.tencent.smtt.sdk.WebViewClient; public class MainActivity extends Activity {public WebView webView; //定义一个变量,来标识是否退出 private static boolean isExit = false; //webView要加载的网址 public String url = "http://www.baidu.com/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去掉activity标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉Android上方信息栏(运营商,信号,时间等信息的那一栏) getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); webView = findViewById(R.id.web_view); //X5内核回调接口 QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {@Override public void onCoreInitFinished() {}@Override public void onViewInitFinished(boolean b) { Log.d("app", "onViewInitFinished is " + b); } }; //X5内核初始化接口 QbSdk.initX5Environment(getApplicationContext(), cb); //WebSettings WebSettings webSettings = webView.getSettings(); //缓存 //允许加载JS webSettings.setJavaScriptEnabled(true); //DOM缓存 webSettings.setDomStorageEnabled(true); //cache缓存 webSettings.setAppCacheEnabled(false); //webView中访问内容URL,默认true webSettings.setAllowContentAccess(true); //自适应屏幕,超出宽度时,会缩小适应屏幕 webSettings.setLoadWithOverviewMode(true); //强行使用WebView打开网址,禁止跳转到手机自带浏览器打开 webView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { //webView自己加载URL,然后通知系统不需要HandleURL view.loadUrl(url); return true; } }); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); }@Override protected void onStart() { super.onStart(); //新线程中运行 runOnUiThread(new Runnable() { @Override public void run() { webView.loadUrl(url); } }); }/** * onCreate() * onStart() * onRestart() * onResume() * onPause() * onStop() * onDestroy() * 为Activity生命周期 **/@Override protected void onRestart() { super.onRestart(); }@Override protected void onResume() { super.onResume(); }@Override protected void onPause() { super.onPause(); }@Override protected void onStop() { super.onStop(); }@Override protected void onDestroy() { super.onDestroy(); }//返回键监听 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { exit(); return false; } return super.onKeyDown(keyCode, event); }private void exit() { if (!isExit) { isExit = true; Toast.makeText(MainActivity.this, "再按一次 退出程序", Toast.LENGTH_SHORT).show(); mHandler.sendEmptyMessageDelayed(0, 1000); } else { finish(); System.exit(0); } }Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); isExit = false; } }; }

代码涉及到的内容有
1.去掉activity标题栏
2.去掉activity信息栏
3.X5浏览器内核Demo(官方地址:https://x5.tencent.com/tbs/guide/sdkInit.html )
4.webView的一些webSettings设置
5.禁止webView跳转到手机自带浏览器
6.返回键监听


代码已经上传到CSDN了,点击前往


    推荐阅读