{"id":1282,"date":"2023-03-18T10:08:12","date_gmt":"2023-03-18T02:08:12","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1282"},"modified":"2023-04-29T09:21:24","modified_gmt":"2023-04-29T01:21:24","slug":"agentweb-calls-jsbridge","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/agentweb-calls-jsbridge\/","title":{"rendered":"AgentWeb\u8c03\u7528JsBridge\u91c7\u5751\u8bb0\u5f55"},"content":{"rendered":"<p>AgentWeb\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/Justson\/AgentWeb\">https:\/\/github.com\/Justson\/AgentWeb<\/a><br \/>\nJsBridge\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/lzyzsd\/JsBridge\">https:\/\/github.com\/lzyzsd\/JsBridge<\/a><\/p>\n<p>JS\u9700\u6dfb\u52a0<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-javascript\">function connectWebViewJavascriptBridge(callback) {\n    if (window.WebViewJavascriptBridge) {\n        callback(WebViewJavascriptBridge)\n    } else {\n        document.addEventListener(\n            &#039;WebViewJavascriptBridgeReady&#039;\n            , function() {\n                callback(WebViewJavascriptBridge)\n            },\n            false\n        );\n    }\n}\n\nconnectWebViewJavascriptBridge(function(bridge) {\n    bridge.init(function(message, responseCallback) {\n        console.log(&#039;Js got a message&#039;, message);\n        var data = {\n            &#039;Javascript Responds&#039;: &#039;\u6d4b\u8bd5\u4e2d\u6587!&#039;\n        };\n        console.log(&#039;Js responding with&#039;, data);\n        responseCallback(data);\n    });\n})<\/code><\/pre>\n<p>WebViewClient<\/p>\n<pre><code class=\"language-java\">private WebViewClient getWebViewClient() {\n    return new WebViewClient() {\n        BridgeWebViewClient mBridgeWebViewClient = new BridgeWebViewClient(mBridgeWebView);\n\n        @RequiresApi(api = Build.VERSION_CODES.N)\n        @Override\n        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {\n            NLog.i(Constants.TAG, &quot;url: &quot; + request.getUrl().toString());\n            return mBridgeWebViewClient.shouldOverrideUrlLoading(view, request);  \/\/\u517c\u5bb9\u9ad8\u7248\u672c\uff0c\u5fc5\u987b\u8bbe\u7f6e\n        }\n\n        @Override\n        public boolean shouldOverrideUrlLoading(WebView view, String url) {\n            NLog.i(Constants.TAG, &quot;url: &quot; + url);\n            return mBridgeWebViewClient.shouldOverrideUrlLoading(view, url);  \/\/\u517c\u5bb9\u4f4e\u7248\u672c\uff0c\u5fc5\u987b\u8bbe\u7f6e\n        }\n\n        @Override\n        public void onPageStarted(WebView view, String url, Bitmap favicon) {\n            mBridgeWebViewClient.onPageStarted(view, url, favicon);  \/\/\u5fc5\u987b\u8bbe\u7f6e\n        }\n\n        @Override\n        public void onPageFinished(WebView view, String url) {  \/\/\u5fc5\u987b\u8bbe\u7f6e\n            mBridgeWebViewClient.onPageFinished(view, url);\n        }\n\n        @TargetApi(Build.VERSION_CODES.M)\n        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)\n        @Override\n        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {\n            super.onReceivedError(view, request, error);\n            \/\/yy:\/\/__QUEUE_MESSAGE__\/\n            LogUtils.i(TAG, &quot;onReceivedError:&quot; + error.getDescription() + &quot; code:&quot; + error.getErrorCode() + &quot;, url: &quot; + request.getUrl().toString());\n        }\n    };\n}<\/code><\/pre>\n<p>\u5fc5\u987b\u8bbe\u7f6eWebViewClient\uff0c\u5e76\u4e14\u5176\u751f\u547d\u5468\u671f\u4ea4\u7531BridgeWebViewClient\u5904\u7406\uff0c\u5426\u5219\u62a5\u9519\uff1a<\/p>\n<pre><code>agentweb\u00a0-\u00a0DefaultWebClient: onReceivedError:net::ERR_UNKNOWN_URL_SCHEME code:-10<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>AgentWeb\uff1ahttps:\/\/github.com\/Justson\/AgentWeb JsBridge\uff1ah [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[320],"class_list":["post-1282","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-jsbridge"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1282"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1282\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}