{"id":5634,"date":"2023-09-12T12:02:25","date_gmt":"2023-09-12T04:02:25","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/?p=5634"},"modified":"2023-09-12T12:02:25","modified_gmt":"2023-09-12T04:02:25","slug":"android-%e5%b0%81%e8%a3%85%e7%bd%91%e9%a1%b5%e6%93%8d%e4%bd%9c%e6%ad%a5%e9%aa%a4%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.net\/zhidianwl\/2023\/09\/12\/android-%e5%b0%81%e8%a3%85%e7%bd%91%e9%a1%b5%e6%93%8d%e4%bd%9c%e6%ad%a5%e9%aa%a4%e4%bb%8b%e7%bb%8d\/","title":{"rendered":"android \u5c01\u88c5\u7f51\u9875\u64cd\u4f5c\u6b65\u9aa4\u4ecb\u7ecd"},"content":{"rendered":"

Android\u5c01\u88c5\u7f51\u9875\u662f\u6307\u5728Android\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49WebView\u63a7\u4ef6\uff0c\u5c06\u7f51\u9875\u52a0\u8f7d\u548c\u663e\u793a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u540c\u65f6\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u63a7\u5236\u7f51\u9875\u7684\u884c\u4e3a\u548c\u4ea4\u4e92\u3002\u8fd9\u79cd\u5c01\u88c5\u65b9\u5f0f\u53ef\u4ee5\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u62e5\u6709\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u548c\u66f4\u9ad8\u7684\u81ea\u5b9a\u4e49\u6027\u3002<\/p>\n

\u4e00\u3001\u81ea\u5b9a\u4e49WebView\u63a7\u4ef6<\/p>\n

\u5728Android\u4e2d\uff0cWebVieandroid\u6df7\u5408\u5f00\u53d1<\/a>w\u662f\u4e00\u4e2a\u63a7\u4ef6\uff0c\u53ef\u4ee5\u7528\u6765\u663e\u793a\u7f51\u9875\u3002\u5982\u679c\u60f3\u8981\u81ea\u5b9a\u4e49WebView\u63a7\u4ef6\uff0c\u9700\u8981\u7ee7\u627fWebView\u7c7b\uff0c\u5e76\u91cd\u5199\u4e00\u4e9b\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6bd4\u8f83\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n

1. loadUrl(String url): \u52a0\u8f7d\u6307\u5b9a\u7684url\u5730\u5740\u7684\u7f51\u9875\u3002<\/p>\n

2. setWebViewClient(WebViewClient client): \u8bbe\u7f6eWebViewClient\u5bf9\u8c61\uff0c\u7528\u4e8e\u5904\u7406WebView\u7684\u5404\u79cd\u4e8b\u4ef6\uff0c\u4f8b\u5982\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u3001\u9875\u9762\u52a0\u8f7d\u5931\u8d25\u3001\u9875\u9762\u8df3\u8f6c\u7b49\u3002<\/p>\n

3. setWebChromeClient(WebChromeClient client): \u8bbe\u7f6eWebChromeClient\u5bf9\u8c61\uff0c\u7528\u4e8e\u5904\u7406WebView\u7684\u5404\u79cd\u754c\u9762\u4e8b\u4ef6\uff0c\u4f8b\u5982\u5f39\u51fa\u5bf9\u8bdd\u6846\u3001\u663e\u793a\u8fdb\u5ea6\u6761\u7b49\u3002<\/p>\n

4. setJavaScriptEnabled(boolean flag): \u8bbe\u7f6e\u662f\u5426\u542f\u7528JavaScript\u811a\u672c\u3002<\/p>\n

5. setCacheMode(int mode): \u8bbe\u7f6e\u7f13\u5b58\u6a21\u5f0f\uff0c\u4f8b\u5982\u4e0d\u4f7f\u7528\u7f13\u5b58\u3001\u53ea\u4f7f\u7528\u7f13\u5b58\u7b49\u3002<\/p>\n

\u4e8c\u3001\u5c01\u88c5\u7f51\u9875\u7684\u6b65\u9aa4<\/p>\n

1. \u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WebView\u63a7\u4ef6\uff0c\u4f8b\u5982MyWebView\u3002<\/p>\n

2. \u5728MyWebView\u4e2d\u91cd\u5199WebViewClient\u548cWebChromeClient\u7684\u76f8\u5173\u65b9\u6cd5\uff0c\u7528\u4e8e\u5904\u7406WebView\u7684\u5404\u79cd\u4e8b\u4ef6\u548c\u754c\u9762\u4e8b\u4ef6\u3002<\/p>\n

3. \u5728MyWebView\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u81ea\u5b9a\u4e49\u65b9\u6cd5\uff0c\u4f8b\u5982loadUrlWithHeader(String url, Map headers)\uff0c\u7528\u4e8e\u5728\u8bf7\u6c42\u7f51\u9875\u65f6\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684HTTP\u5934\u90e8\u4fe1\u606f\u3002<\/p>\n

4. \u5728MyWebView\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u4f8b\u5982isSupportZoom(boolean flag)\uff0c\u7528\u4e8e\u63a7\u5236\u7f51\u9875\u662f\u5426\u652f\u6301\u7f29\u653e\u3002<\/p>\n

5. \u5728MyWebView\u4e2d\u5c01\u88c5\u4e00\u4e9b\u5e38\u7528\u7684\u4ea4\u4e92\u65b9\u6cd5\uff0c\u4f8b\u5982showToast(String message)\uff0c\u7528\u4e8e\u5728\u7f51\u9875\u4e2d\u663e\u793aToast\u6d88\u606f\u3002<\/p>\n

6. \u5728MyWebView\u7684\u5916\u90e8\u5c01\u88c5\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982loadUrl(String url)\uff0c\u7528\u4e8e\u52a0\u8f7d\u6307\u5b9a\u7684url\u5730\u5740\u7684\u7f51\u9875\uff0c\u540c\u65f6\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528MyWebView\u7684\u76f8\u5173\u65b9\u6cd5\u6765\u63a7\u5236\u7f51\u9875\u7684\u884c\u4e3a\u548c\u4ea4\u4e92\u3002<\/p>\n

\u4e09\u3001\u5c01\u88c5\u7f51\u9875\u7684\u5b9e\u73b0<\/p>\n

\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5c01\u88c5\u7f51\u9875\u7684\u5b9e\u73b0\u793a\u4f8b\uff1a<\/p>\n

1. \u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WebView\u63a7\u4ef6MyWebView\uff0c\u7ee7\u627f\u81eaWebView\u7c7b\u3002<\/p>\n

“`<\/p>\n

public class MyWebView extends WebView {<\/p>\n

private boolean isSupportZoom = true;<\/p>\n

public MyWebView(Context context) {<\/p>\n

super(context);<\/p>\n

init();<\/p>\n

}<\/p>\n

public MyWebView(Context context, AttributeSet attrs) {<\/p>\n

super(context, attrs);<\/p>\n

init();<\/p>\n

}<\/p>\n

public MyWebView(Context context, AttributeSet attrs, int defStyle) {<\/p>\n

super(context, attrs, defStyle);<\/p>\n

init();<\/p>\n

}<\/p>\n

private void init() {<\/p>\n

WebSettings webSettings = getSe<\/p>\n

<\/figure>\n<\/p>\n

ttings();<\/p>\n

webSettings.setJavaScriptEnabled(true);<\/p>\n

h5\u6253\u5305ios\u4e0a\u67b6<\/a> webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);<\/p>\n

setWebViewClient(new MyWebViewClient());<\/p>\n

setWebChromeClient(new MyWebChromeClient());<\/p>\n

}<\/p>\n

public void loadUrlWithHeader(String url, Map headers) {<\/p>\n

if (url == null) {<\/p>\n

return;<\/p>\n

}<\/p>\n

if (headers == null || headers.isEmpty()) {<\/p>\n

loadUrl(url);<\/p>\n

return;<\/p>\n

}<\/p>\n

String cookie = headers.get(“Cookie”);<\/p>\n

if (!TextUtils.isEmpty(cookie)) {<\/p>\n

CookieManager.getInstance().setCookie(url, cookie);<\/p>\n

}<\/p>\n

String userAgent = headers.get(“User-Agent”);<\/p>\n

if (!TextUtils.isEmpty(userAgent)) {<\/p>\n

getSettings().setUserAgentString(userAgent);<\/p>\n

}<\/p>\n

loadUrl(url);<\/p>\n

}<\/p>\n

public void showToast(String message) {<\/p>\n

Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();<\/p>\n

}<\/p>\n

public void setIsSupportZoom(boolean flag) {<\/p>\n

isSupportZoom = flag;<\/p>\n

getSettings().setSupportZoom(flag);<\/p>\n

}<\/p>\n

private class MyWebViewClient extends WebViewClient {<\/p>\n

@Override<\/p>\n

public boolean shouldOverrideUrlLoading(WebView view, String url) {<\/p>\n

if (url.startsWith(“http:\/\/”) || url.startsWith(“https:\/\/”)) {<\/p>\n

view.loadUrl(url);<\/p>\n

return true;<\/p>\n

}<\/p>\n

return false;<\/p>\n

}<\/p>\n

@Override<\/p>\n

public void onPageStarted(WebView view, String url, Bitmap favicon) {<\/p>\n

\/\/ \u9875\u9762\u5f00\u59cb\u52a0\u8f7d<\/p>\n

}<\/p>\n

@Override<\/p>\n

public void onPageFinished(WebView view, String url) {<\/p>\n

\/\/ \u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/p>\n

}<\/p>\n

@Override<\/p>\n

public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {<\/p>\n

\/\/ \u9875\u9762\u52a0\u8f7d\u5931\u8d25<\/p>\n

}<\/p>\n

}<\/p>\n

private class MyWebChromeClient extends WebChromeClient {<\/p>\n

@Override<\/p>\n

public boolean onJsAlert(WebView view, String url, String message, JsResult result) {<\/p>\n

\/\/ \u5f39\u51fa\u5bf9\u8bdd\u6846<\/p>\n

return true;<\/p>\n

}<\/p>\n

@Override<\/p>\n

public void onProgressChanged(WebView view, int newProgress) {<\/p>\n

\/\/ \u663e\u793a\u8fdb\u5ea6\u6761<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

2. \u5728MyWebView\u7684\u5916\u90e8\u5c01\u88c5\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982loadUrl(String url)\uff0c\u7528\u4e8e\u52a0\u8f7d\u6307\u5b9a\u7684url\u5730\u5740\u7684\u7f51\u9875\uff0c\u540c\u65f6\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528MyWebView\u7684\u76f8\u5173\u65b9\u6cd5\u6765\u63a7\u5236\u7f51\u9875\u7684\u884c\u4e3a\u548c\u4ea4\u4e92\u3002<\/p>\n

“`<\/p>\n

public class WebViewActivity extends AppCompatActivity {<\/p>\n

private MyWebView mWebView;<\/p>\n

@Override<\/p>\n

protected void onCreate(Bundle savedInstanceState) {<\/p>\n

super.onCreate(savedInstanceState);<\/p>\n

setContentView(R.layout.activity_web_view);<\/p>\n

mWebView = findViewById(R.id.web_view);<\/p>\n

mWebView.setIsSupportZoom(false);<\/p>\n

mWebView.loadUrl(“https:\/\/www.baidu.com”);<\/p>\n

}<\/p>\n

@Override<\/p>\n

public boolean onKeyDown(int keyCode, KeyEvent event) {<\/p>\n

if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {<\/p>\n

mWebView.goBack();<\/p>\n

return true;<\/p>\n

}<\/p>\n

return super.onKeyDown(keyCode, event);<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u4ee5\u4e0a\u793a\u4f8b\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5c01\u88c5\u7f51\u9875\u7684\u5b9e\u73b0\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u80fd\u9700\u8981\u66f4\u590d\u6742\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u603b\u7684\u6765\u8bf4\uff0c\u5c01\u88c5\u7f51\u9875\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u4f53\u9a8c\u548c\u81ea\u5b9a\u4e49\u6027\uff0c\u540c\u65f6\u4e5f\u9700\u8981\u8003\u8651\u5230\u4e00\u4e9b\u5b89\u5168\u548c\u6027\u80fd\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Android\u5c01\u88c5\u7f51\u9875\u662f\u6307\u5728Android\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49WebView\u63a7\u4ef6\uff0c\u5c06\u7f51\u9875\u52a0\u8f7d\u548c\u663e\u793a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u540c\u65f6\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u63a7\u5236\u7f51\u9875\u7684\u884c\u4e3a\u548c\u4ea4\u4e92\u3002\u8fd9\u79cd\u5c01\u88c5\u65b9\u5f0f\u53ef\u4ee5\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u62e5\u6709\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c<\/p>\n","protected":false},"author":23,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[674,5077,414,10377,356],"topic":[],"class_list":["post-5634","post","type-post","status-publish","format-standard","hentry","category-kaifaapp","tag-674","tag-5077","tag-414","tag-apk","tag-356"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/5634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=5634"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/5634\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=5634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=5634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=5634"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=5634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}