44import android .webkit .*;
55import android .widget .Toast ;
66import com .example .WebViewJavascriptBridgeExample .R ;
7+ import org .json .JSONObject ;
8+
79import java .io .IOException ;
810import java .io .InputStream ;
11+ import java .util .HashMap ;
12+ import java .util .Map ;
913import 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