Skip to content

Commit 52b94a4

Browse files
committed
have error
1 parent 9e78708 commit 52b94a4

File tree

4 files changed

+77
-30
lines changed

4 files changed

+77
-30
lines changed

Android/Example/res/raw/sample.html

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<html>
2+
<input type="button" value="Test" onClick="test()"/>
3+
4+
<script type="text/javascript">
5+
function test(){
6+
alert("test");
7+
}
8+
console.log("user_client.js called!");
9+
document.addEventListener('WebViewJavascriptBridgeReady'
10+
, function(event) {
11+
var bridge=event.bridge;
12+
bridge.init(function(message, responseCallback) {
13+
alert('Received message: ' + message);
14+
console.log("responseCallback:"+responseCallback);
15+
if (responseCallback) {
16+
17+
responseCallback("Right back atcha") ;
18+
}
19+
}) ;
20+
bridge.send('Hello from the javascript');
21+
22+
}, false)
23+
</script>
24+
</html>

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
public class MyActivity extends Activity {
1212
private WebView webView;
13+
private WebViewJavascriptBridge bridge;
1314

1415
/**
1516
* Called when the activity is first created.
@@ -19,22 +20,34 @@ public void onCreate(Bundle savedInstanceState) {
1920
super.onCreate(savedInstanceState);
2021
setContentView(R.layout.main);
2122
webView=(WebView) this.findViewById(R.id.webView);
22-
WebViewJavascriptBridge bridge=
23-
new WebViewJavascriptBridge(this.getApplicationContext(),webView,new UserHandler()) ;
24-
loadSample();
23+
bridge=
24+
new WebViewJavascriptBridge(this.getApplicationContext(),webView,new UserServerHandler()) ;
25+
loadUserClient();
2526
}
2627

27-
private void loadSample(){
28-
InputStream is=getResources().openRawResource(R.raw.sample);
29-
String sample=WebViewJavascriptBridge.convertStreamToString(is);
30-
webView.loadData(sample,"text/html","UTF-8");
28+
private void loadUserClient(){
29+
InputStream is=getResources().openRawResource(R.raw.user_client);
30+
String user_client_html=WebViewJavascriptBridge.convertStreamToString(is);
31+
webView.loadData(user_client_html,"text/html","UTF-8");
3132
}
3233

33-
class UserHandler implements WebViewJavascriptBridge.WVJBHandler{
34+
class UserServerHandler implements WebViewJavascriptBridge.WVJBHandler{
3435
@Override
3536
public void handle(String data, WebViewJavascriptBridge.WVJBResponseCallback jsCallback) {
36-
Log.d("test", data);
37-
jsCallback.callback("java said received:"+data);
37+
Log.d("test","Received message from javascript: "+ data);
38+
if (null !=jsCallback) {
39+
jsCallback.callback("Right back atcha");
40+
}
41+
42+
43+
bridge.send("I expect a response!",new WebViewJavascriptBridge.WVJBResponseCallback() {
44+
@Override
45+
public void callback(String responseData) {
46+
Log.d("test","Got response! "+responseData);
47+
}
48+
});
49+
bridge.send("Hi");
3850
}
3951
}
52+
4053
}

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.HashMap;
1212
import java.util.Map;
1313
import java.util.Scanner;
14+
import java.util.concurrent.BlockingQueue;
15+
import java.util.concurrent.LinkedBlockingQueue;
1416

1517
/**
1618
* Created with IntelliJ IDEA.
@@ -26,13 +28,16 @@ public class WebViewJavascriptBridge {
2628
Map<String,WVJBHandler> _messageHandlers;
2729
Map<String,WVJBResponseCallback> _responseCallbacks;
2830
long _uniqueId;
31+
BlockingQueue<String> _messageQueue;
32+
2933
public WebViewJavascriptBridge(Context context,WebView webview,WVJBHandler handler) {
3034
this.mContext=context;
3135
this.mWebView=webview;
3236
this._messageHandler=handler;
3337
_messageHandlers=new HashMap<String,WVJBHandler>();
3438
_responseCallbacks=new HashMap<String, WVJBResponseCallback>();
3539
_uniqueId=0;
40+
_messageQueue=new LinkedBlockingQueue<String>();
3641
WebSettings webSettings = mWebView.getSettings();
3742
webSettings.setJavaScriptEnabled(true);
3843
mWebView.addJavascriptInterface(this, "_WebViewJavascriptBridge");
@@ -71,9 +76,7 @@ public void onPageFinished(WebView webView, String url) {
7176
private class MyWebChromeClient extends WebChromeClient {
7277
@Override
7378
public boolean onConsoleMessage(ConsoleMessage cm) {
74-
Log.d("test", cm.message() + " -- From line "
75-
+ cm.lineNumber() + " of "
76-
+ cm.sourceId() );
79+
Log.d("test", cm.message());
7780
return true;
7881
}
7982

@@ -161,14 +164,36 @@ private void _sendData(String data,WVJBResponseCallback responseCallback,String
161164
if (null!=handlerName) {
162165
message.put("handlerName", handlerName);
163166
}
164-
_dispatchMessage(message);
167+
_queueMessage(message);
168+
}
169+
170+
private void _queueMessage(Map<String, String> message) {
171+
String messageJSON = new JSONObject(message).toString();
172+
try {
173+
_messageQueue.put(messageJSON);
174+
_notifyNewMessage();
175+
} catch (InterruptedException e) {
176+
Log.e("test",e.getMessage());
177+
e.printStackTrace();
178+
}
179+
}
180+
181+
@JavascriptInterface
182+
public String _getQueuedMessage(){
183+
return _messageQueue.poll();
184+
}
185+
186+
private void _notifyNewMessage() {
187+
String javascriptCommand =
188+
"javascript:WebViewJavascriptBridge._getNewMessageFromJava();";
189+
mWebView.loadUrl(javascriptCommand);
165190
}
166191

167192
private void _dispatchMessage(Map <String, String> message){
168193
String messageJSON = new JSONObject(message).toString();
169194
Log.d("test","sending:"+messageJSON);
170195
String javascriptCommand =
171-
String.format("javascript:WebViewJavascriptBridge._handleMessageFromOJava('%s');",messageJSON);
196+
String.format("javascript:WebViewJavascriptBridge._handleMessageFromJava('%s');",messageJSON);
172197
mWebView.loadUrl(javascriptCommand);
173198
}
174199

0 commit comments

Comments
 (0)