|
1 | 1 | package com.leo;
|
2 | 2 |
|
| 3 | +import android.content.Context; |
3 | 4 | import android.content.Intent;
|
4 | 5 | import android.databinding.DataBindingUtil;
|
| 6 | +import android.net.Uri; |
| 7 | +import android.net.http.SslError; |
| 8 | +import android.os.Build; |
5 | 9 | import android.os.Bundle;
|
6 | 10 | import android.support.annotation.Nullable;
|
7 | 11 | import android.support.v7.app.AppCompatActivity;
|
| 12 | +import android.view.KeyEvent; |
8 | 13 | import android.view.View;
|
| 14 | +import android.webkit.JavascriptInterface; |
| 15 | +import android.webkit.SslErrorHandler; |
| 16 | +import android.webkit.WebChromeClient; |
| 17 | +import android.webkit.WebSettings; |
| 18 | +import android.webkit.WebView; |
| 19 | +import android.webkit.WebViewClient; |
| 20 | +import android.widget.Toast; |
9 | 21 |
|
10 |
| -import com.leo.databinding.ActivityWikiBinding; |
| 22 | +import com.leo.databinding.ActivityWikiWebBinding; |
11 | 23 | import com.lihang.ShadowLayout;
|
12 | 24 |
|
13 | 25 | /**
|
14 | 26 | * Created by leo
|
15 | 27 | * on 2020/8/5.
|
16 | 28 | */
|
17 | 29 | public class WikiActivity extends AppCompatActivity {
|
18 |
| - ActivityWikiBinding binding; |
| 30 | + ActivityWikiWebBinding binding; |
| 31 | + String urlStr; |
19 | 32 |
|
20 | 33 | @Override
|
21 | 34 | protected void onCreate(@Nullable Bundle savedInstanceState) {
|
22 | 35 | super.onCreate(savedInstanceState);
|
23 |
| - binding = DataBindingUtil.setContentView(this, R.layout.activity_wiki); |
| 36 | + binding = DataBindingUtil.setContentView(this, R.layout.activity_wiki_web); |
| 37 | + |
| 38 | + |
| 39 | + // urlStr = "https://github.com/lihangleo2"; |
| 40 | + urlStr = "https://github.com/lihangleo2/ShadowLayout/wiki"; |
| 41 | + //声明WebSettings子类 |
| 42 | + WebSettings webSettings = binding.webView.getSettings(); |
| 43 | + |
| 44 | + webSettings.setJavaScriptEnabled(true); |
| 45 | + //设置自适应屏幕,两者合用 |
| 46 | + webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小 |
| 47 | + webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小 |
| 48 | + webSettings.setDomStorageEnabled(true); |
| 49 | + |
| 50 | + //缩放操作 |
| 51 | + webSettings.setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。 |
| 52 | + webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false,则该WebView不可缩放 |
| 53 | + webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件 |
| 54 | + |
| 55 | + //其他细节操作 |
| 56 | + webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //开启缓存LOAD_CACHE_ELSE_NETWORK//LOAD_NO_CACHE关闭缓存 |
| 57 | + webSettings.setAllowFileAccess(true); //设置可以访问文件 |
| 58 | + webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口 |
| 59 | + webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片 |
| 60 | + webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式 |
| 61 | + |
| 62 | + |
| 63 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 64 | + //解决webView不加载图片 |
| 65 | + webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + ////步骤3. 复写shouldOverrideUrlLoading()方法,使得打开网页时不调用系统浏览器, 而是在本WebView中显示 |
| 70 | + binding.webView.setWebViewClient(new WebViewClient() { |
| 71 | + @Override |
| 72 | + public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 73 | + try { |
| 74 | + if (url.startsWith("http:") || url.startsWith("https:")) { |
| 75 | + view.loadUrl(url); |
| 76 | + return false; |
| 77 | + } else { |
| 78 | + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 79 | + startActivity(intent); |
| 80 | + return true; |
| 81 | + } |
| 82 | + } catch (Exception e) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
| 90 | + |
| 91 | + |
| 92 | + if (binding.progressBar != null) { |
| 93 | + binding.progressBar.setVisibility(View.GONE); |
| 94 | + } |
| 95 | + |
| 96 | + Toast.makeText(WikiActivity.this, "网页加载失败", Toast.LENGTH_SHORT).show(); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | +// onReceivedSslError |
| 102 | + |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { |
| 106 | + |
| 107 | + handler.proceed(); |
| 108 | + } |
| 109 | + |
| 110 | + //webView加载结束后的动作 |
| 111 | + @Override |
| 112 | + public void onPageFinished(WebView view, String url) { |
| 113 | + //设定加载结束的操作 |
| 114 | + if (binding.progressBar != null) { |
| 115 | + binding.progressBar.setVisibility(View.GONE); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + |
| 123 | + binding.webView.setWebChromeClient(new WebChromeClient() { |
| 124 | + |
| 125 | + @Override |
| 126 | + public void onProgressChanged(WebView view, int newProgress) { |
| 127 | + if (binding.progressBar.getVisibility() == View.GONE && newProgress != 100) { |
| 128 | + //点击webView内部,进度条可见 |
| 129 | + binding.progressBar.setVisibility(View.VISIBLE); |
| 130 | + } |
| 131 | + |
| 132 | + if (newProgress == 100 && binding.progressBar.getVisibility() == View.VISIBLE) { |
| 133 | + //bug:不走finish所以不能设置不可见 |
| 134 | + binding.progressBar.setVisibility(View.GONE); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + if (binding.progressBar != null) { |
| 139 | + binding.progressBar.setProgress(newProgress); |
| 140 | + } |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + |
| 145 | + binding.webView.loadUrl(urlStr); |
| 146 | + //js互调相关 |
| 147 | + binding.webView.addJavascriptInterface(new CrosswalkInterface(this), "JSbridge"); |
| 148 | + |
| 149 | + |
24 | 150 | binding.shadowLayoutBarLeft.setOnClickListener(v -> {
|
25 |
| - finish(); |
| 151 | + if (binding.webView.canGoBack()) { |
| 152 | + binding.webView.goBack(); |
| 153 | + } else { |
| 154 | + finish(); |
| 155 | + } |
26 | 156 | });
|
| 157 | + |
27 | 158 | }
|
| 159 | + |
| 160 | + |
| 161 | + @Override |
| 162 | + public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 163 | + if (keyCode == KeyEvent.KEYCODE_BACK && binding.webView.canGoBack()) { |
| 164 | + binding.webView.goBack();// 返回前一个页面 |
| 165 | + return true; |
| 166 | + } |
| 167 | + return super.onKeyDown(keyCode, event); |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + @Override |
| 172 | + protected void onResume() { |
| 173 | + super.onResume(); |
| 174 | + if (binding.webView != null) { |
| 175 | + binding.webView.onResume(); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + protected void onPause() { |
| 181 | + super.onPause(); |
| 182 | + if (binding.webView != null) { |
| 183 | + binding.webView.onPause(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + protected void onDestroy() { |
| 189 | + super.onDestroy(); |
| 190 | + if (binding.webView != null) { |
| 191 | + binding.webView.destroy(); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + public class CrosswalkInterface { |
| 196 | + Context context; |
| 197 | + |
| 198 | + public CrosswalkInterface(Context context) { |
| 199 | + this.context = context; |
| 200 | + } |
| 201 | + |
| 202 | + @JavascriptInterface |
| 203 | + public void exit() { |
| 204 | + finish(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + |
28 | 209 | }
|
0 commit comments