Skip to content

Commit f5e9530

Browse files
committed
java nearly work
1 parent e05a81b commit f5e9530

File tree

5 files changed

+141
-19
lines changed

5 files changed

+141
-19
lines changed

Android/Example/design/design.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Java init:
22
WebViewJavascriptBridge bridge = new WebViewJavascriptBridge(Webview webview,WVJBHandler handler);
33

44
Interface WVJBHandler {
5-
public void handle(String data,long jsCallbackId);
5+
public void handle(String data,WVJBResponseCallback jsCallback);
66
}
77

88
Interface WVJBResponseCallback{
@@ -57,7 +57,7 @@ function getJavaResponseCallback( javaCallbackId){
5757

5858

5959
java:
60-
1) 打包为json发送
60+
1) 打包为json发送 WebViewJavascriptBridge._handleMessageFromJava(messageJSON);[js]
6161
2) 解析分发:
6262
var responseCallback = responseCallbacks[message.responseId]
6363
responseCallback(message.responseData)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
WebViewJavascriptBridge._handleMessageFromJava=function(messageJSON){
2+
console.log(messageJSON);
3+
var json=JSON.parse(messageJSON);
4+
for(var k in json){
5+
console.log(json[k]);
6+
}
7+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<html>
2-
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
2+
<input type="button" value="Say hello" onClick="test()" />
33

44
<script type="text/javascript">
55
function showAndroidToast(toast) {
66
alert(toast);
77
console.log(toast);
88
}
9+
function test(){
10+
var x={};x.a=1;x.b=3;
11+
msg=JSON.stringify(x);
12+
WebViewJavascriptBridge._handleMessageFromJava( msg);
13+
}
914
</script>
1015
</html>

Android/Example/src/com/example/WebViewJavascriptBridgeExample/MyActivity.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.os.Bundle;
5+
import android.util.Log;
56
import android.webkit.WebView;
67
import com.fangjian.WebViewJavascriptBridge;
78

@@ -18,7 +19,8 @@ public void onCreate(Bundle savedInstanceState) {
1819
super.onCreate(savedInstanceState);
1920
setContentView(R.layout.main);
2021
webView=(WebView) this.findViewById(R.id.webView);
21-
WebViewJavascriptBridge bridge=new WebViewJavascriptBridge(this.getApplicationContext(),webView) ;
22+
WebViewJavascriptBridge bridge=
23+
new WebViewJavascriptBridge(this.getApplicationContext(),webView,new UserHandler()) ;
2224
loadSample();
2325
}
2426

@@ -27,4 +29,12 @@ private void loadSample(){
2729
String sample=WebViewJavascriptBridge.convertStreamToString(is);
2830
webView.loadData(sample,"text/html","UTF-8");
2931
}
32+
33+
class UserHandler implements WebViewJavascriptBridge.WVJBHandler{
34+
@Override
35+
public void handle(String data, WebViewJavascriptBridge.WVJBResponseCallback jsCallback) {
36+
Log.d("test", data);
37+
jsCallback.callback("java said received:"+data);
38+
}
39+
}
3040
}

Android/Example/src/com/fangjian/WebViewJavascriptBridge.java

Lines changed: 115 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import android.webkit.*;
55
import android.widget.Toast;
66
import com.example.WebViewJavascriptBridgeExample.R;
7+
import org.json.JSONObject;
8+
79
import java.io.IOException;
810
import java.io.InputStream;
11+
import java.util.HashMap;
12+
import java.util.Map;
913
import java.util.Scanner;
1014

1115
/**
@@ -18,29 +22,24 @@ public class WebViewJavascriptBridge {
1822

1923
WebView mWebView;
2024
Context mContext;
21-
public WebViewJavascriptBridge(Context context,WebView webview) {
25+
WVJBHandler _messageHandler;
26+
Map<String,WVJBHandler> _messageHandlers;
27+
Map<String,WVJBResponseCallback> _responseCallbacks;
28+
long _uniqueId;
29+
public WebViewJavascriptBridge(Context context,WebView webview,WVJBHandler handler) {
2230
this.mContext=context;
2331
this.mWebView=webview;
32+
this._messageHandler=handler;
33+
_messageHandlers=new HashMap<String,WVJBHandler>();
34+
_responseCallbacks=new HashMap<String, WVJBResponseCallback>();
35+
_uniqueId=0;
2436
WebSettings webSettings = mWebView.getSettings();
2537
webSettings.setJavaScriptEnabled(true);
2638
mWebView.addJavascriptInterface(this, "WebViewJavascriptBridge");
2739
mWebView.setWebViewClient(new MyWebViewClient());
2840
mWebView.setWebChromeClient(new MyWebChromeClient()); //optional, for show console and alert
2941
}
3042

31-
@JavascriptInterface
32-
public void callJava(Integer callbackId,String message){
33-
Log.i("test", message + " callbackId:" + callbackId); //id为0,等同null
34-
Log.i("test", "message is null?"+(message== null) ) ;
35-
Log.i("test", "id is null?"+(callbackId == null) ) ;
36-
}
37-
38-
39-
/** Show a toast from the web page */
40-
@JavascriptInterface
41-
public void showToast(String toast) {
42-
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
43-
}
4443

4544
private void loadWebViewJavascriptBridgeJs(WebView webView) {
4645
InputStream is=mContext.getResources().openRawResource(R.raw.webviewjavascriptbridge);
@@ -80,8 +79,109 @@ public boolean onConsoleMessage(ConsoleMessage cm) {
8079

8180
@Override
8281
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
83-
showToast(message);
82+
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
8483
return true;
8584
}
8685
}
86+
87+
public interface WVJBHandler{
88+
public void handle(String data,WVJBResponseCallback jsCallback);
89+
}
90+
91+
public interface WVJBResponseCallback{
92+
public void callback(String data);
93+
}
94+
95+
public void registerHandler(String handlerName,WVJBHandler handler) {
96+
_messageHandlers.put(handlerName,handler);
97+
}
98+
99+
private class CallbackJs implements WVJBResponseCallback{
100+
private final String callbackIJs;
101+
102+
public CallbackJs(String callbackIJs){
103+
this.callbackIJs=callbackIJs;
104+
}
105+
@Override
106+
public void callback(String data) {
107+
_callbackJs(callbackIJs,data);
108+
}
109+
}
110+
111+
112+
private void _callbackJs(String callbackIJs,String data) {
113+
}
114+
115+
@JavascriptInterface
116+
public void _handleMessageFromJs(String data,String responseId,
117+
String responseData,String callbackId,String handlerName){
118+
if (null!=responseId) {
119+
WVJBResponseCallback responseCallback = _responseCallbacks.get(responseId);
120+
responseCallback.callback(responseData);
121+
_responseCallbacks.remove(responseId);
122+
} else {
123+
WVJBResponseCallback responseCallback = null;
124+
if (null!=callbackId) {
125+
responseCallback=new CallbackJs(callbackId);
126+
}
127+
WVJBHandler handler;
128+
if (null!=handlerName) {
129+
handler = _messageHandlers.get(handlerName);
130+
if (null==handler) {
131+
Log.e("test","WVJB Warning: No handler for "+handlerName);
132+
return ;
133+
}
134+
} else {
135+
handler = _messageHandler;
136+
}
137+
try {
138+
handler.handle(data, responseCallback);
139+
}catch (Exception exception) {
140+
Log.e("test","WebViewJavascriptBridge: WARNING: java handler threw. "+exception.getMessage());
141+
}
142+
}
143+
}
144+
145+
public void send(String data) {
146+
send(data,null);
147+
}
148+
149+
public void send(String data ,WVJBResponseCallback responseCallback) {
150+
_sendData(data,responseCallback,null);
151+
}
152+
153+
private void _sendData(String data,WVJBResponseCallback responseCallback,String handlerName){
154+
Map <String, String> message=new HashMap<String,String>();
155+
message.put("data",data);
156+
if (null!=responseCallback) {
157+
String callbackId = "java_cb_"+ (++_uniqueId);
158+
_responseCallbacks.put(callbackId,responseCallback);
159+
message.put("callbackId",callbackId);
160+
}
161+
if (null!=handlerName) {
162+
message.put("handlerName", handlerName);
163+
}
164+
_dispatchMessage(message);
165+
}
166+
167+
private void _dispatchMessage(Map <String, String> message){
168+
String messageJSON = new JSONObject(message).toString();
169+
Log.d("test","sending:"+messageJSON);
170+
String javascriptCommand =
171+
String.format("javascript:WebViewJavascriptBridge._handleMessageFromOJava('%s');",messageJSON);
172+
mWebView.loadUrl(javascriptCommand);
173+
}
174+
175+
public void callHandler(String handlerName) {
176+
callHandler(handlerName, null, null);
177+
}
178+
179+
public void callHandler(String handlerName,String data) {
180+
callHandler(handlerName, data,null);
181+
}
182+
183+
public void callHandler(String handlerName,String data,WVJBResponseCallback responseCallback){
184+
_sendData(data, responseCallback,handlerName);
185+
}
186+
87187
}

0 commit comments

Comments
 (0)