Android WebView First Project
2021-06-16 20:01:41
Android
209

Android WebView First Project


import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.webkit.WebChromeClient; import android.webkit.WebResourceError; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏标题栏 setContentView(R.layout.activity_main); //ActionBar: https://blog.csdn.net/qq_42018605/article/details/105890115 ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.hide(); } // 修改状态栏背景颜色 Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // window.setStatusBarColor(Color.parseColor("#ffffff")); window.setStatusBarColor(Color.TRANSPARENT); // 设置状态栏字体颜色 View stateView = getWindow().getDecorView(); if (stateView != null) { int vis = stateView.getSystemUiVisibility(); vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; //黑色 //vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; //白色 stateView.setSystemUiVisibility(vis); } // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, // WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏 webView = (WebView) findViewById(R.id.wv_webview); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存 webSettings.setAllowFileAccess(true); //设置可以访问文件 webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口 webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片 webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式 webView.loadUrl("https://jxva.com"); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { view.loadUrl(request.getUrl().toString()); } else { view.loadUrl(request.toString()); } return true; } @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { switch (error.getErrorCode()) { case 404: view.loadUrl("file:///android_assets/404.html"); break; default: view.loadUrl("file:///android_assets/50x.html"); break; } } /* @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } */ }); webView.setWebChromeClient(new WebChromeClient() { @Override public void onReceivedTitle(WebView view, String title) { // actionBar.setTitle(title); } }); } //点击返回上一页面而不是退出浏览器 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) { webView.goBack(); return true; } return super.onKeyDown(keyCode, event); } //销毁Webview @Override protected void onDestroy() { if (webView != null) { webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null); webView.clearHistory(); ((ViewGroup) webView.getParent()).removeView(webView); webView.destroy(); webView = null; } super.onDestroy(); } }

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

activity_main.xml

 <WebView
        android:id="@+id/wv_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />