The interaction between html and java, using the jsBridge open-source framework, jsbridge open-source
In html, js registration listening and callback
Function connectWebViewJavascriptBridge (callback) {if (window. webViewJavascriptBridge) {callback (WebViewJavascriptBridge)} else {document. addEventListener ('webviewjavascriptbridgeready', function () {callback (WebViewJavascriptBridge)}, false) ;}} connectWebViewJavascriptBridge (function (bridge) {bridge. init (function (message, responseCallback) {alert (the message received by 'init method js: '+ message); if (message. indexOf ("url =")> = 0) {var url = message. substring (4, message. length); alert ("received url:" + url); document. getElementById ("show "). innerHTML = "this is an image
} });
Bridge. registerHandler ("functionInJs", function (data, responseCallback) {// "submitFromWeb" is the protocol string negotiated between html and java.
Alert (the message received by 'registerhandler method js: '+ data );
If ('java call js' = data ){
ShowAlert ("js method ");
}
If (responseCallback ){
ResponseCallback ('js returned message'); // return of js, charged by java
}
});
Java, registration listening and callback
WebView. setDefaultHandler (new DefaultHandler () {@ Override public void handler (String data, CallBackFunction function) {Log. d (TAG, "setDefaultHandler method message received by java:" + data); if ("openFile ". equals (data) {pickFile (); return;} if (function! = Null) {function. onCallBack ("message returned by java"); // return of java, received by js
}}}); WebView. setWebChromeClient (mOpenFileWebChromeClient); // The operation object to be registered for webview
WebView. registerHandler ("submitFromWeb", new BridgeHandler () {// "submitFromWeb" is the protocol string for html and java negotiation.
@ Override
Public void handler (String data, CallBackFunction function ){
Log. I (TAG, "message received by the registerHandler method java:" + data );
Function. onCallBack ("message returned by java"); // return from java, received by js
}
});
Java sends messages to js without receiving js responses
WebView. send ("java messages sent to js ");
Java sends messages to js and receives js responses.
If (button. equals (v) {webView. callHandler ("functionInJs", "java calls js", new CallBackFunction () {@ Override public void onCallBack (String data) {// TODO Auto-generated method stub Log. I (TAG, "js returned message:" + data );}});
Js sends messages to java without receiving java responses
Function testClick () {var data = "messages sent by js"; window. WebViewJavascriptBridge. send (data );}
Js sends messages to java and receives the java response
Function testClick33 () {var data = "messages sent by js"; window. webViewJavascriptBridge. send (data, function (responseData) {alert ('java returned message: '+ responseData)});} function testClick1 () {window. webViewJavascriptBridge. callHandler ('submitfromweb', 'js calls java method', function (responseData) {alert ('java returned message: '+ responseData )});}
Opening file writing in js
<Input type = "file" value = "SELECT file"/>
In java, you need to set
webView.setWebChromeClient(mOpenFileWebChromeClient);
OpenFileWebChromeClient class
Public class OpenFileWebChromeClient extends WebChromeClient {public static final int callback = 1; public ValueCallback <Uri> mFilePathCallback; public ValueCallback <Uri []> callback; Activity mContext; public OpenFileWebChromeClient) {super (); this. mContext = mContext;} // Android <3.0 calls this method public void openFileChooser (ValueCallback <Uri> filePathCallback) {mFilePathCallback = filePathCallback; Intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. addCategory (Intent. CATEGORY_OPENABLE); intent. setType ("*/*"); mContext. startActivityForResult (Intent. createChooser (intent, "File Chooser"), success);} // 3.0 + call this method public void openFileChooser (ValueCallback filePathCallback, String acceptType) {mFilePathCallback = filePathCallback; intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. addCategory (Intent. CATEGORY_OPENABLE); intent. setType ("*/*"); mContext. startActivityForResult (Intent. createChooser (intent, "File Chooser"), REQUEST_FILE_PICKER );} /// <input type = "file" name = "fileField" id = "fileField"/> event capture // Android> 4.1.1 call this method public void openFileChooser (valueCallback <Uri> filePathCallback, string acceptType, String capture) {mFilePathCallback = filePathCallback; Intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. addCategory (Intent. CATEGORY_OPENABLE); intent. setType ("*/*"); mContext. startActivityForResult (Intent. createChooser (intent, "File Chooser"), REQUEST_FILE_PICKER);} @ Override public boolean onShowFileChooser (WebView webView, ValueCallback <Uri []> filePathCallback, WebChromeClient. fileChooserParams fileChooserParams) {mFilePathCallbacks = filePathCallback; Intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. addCategory (Intent. CATEGORY_OPENABLE); intent. setType ("*/*"); mContext. startActivityForResult (Intent. createChooser (intent, "File Chooser"), REQUEST_FILE_PICKER); return true ;}}
In java, the onActivityResult method of the current activity
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == OpenFileWebChromeClient.REQUEST_FILE_PICKER) { if (mOpenFileWebChromeClient.mFilePathCallback != null) { Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData(); if (result != null) { String path = MediaUtility.getPath(getApplicationContext(), result); Uri uri = Uri.fromFile(new File(path)); mOpenFileWebChromeClient.mFilePathCallback .onReceiveValue(uri); } else { mOpenFileWebChromeClient.mFilePathCallback .onReceiveValue(null); } } if (mOpenFileWebChromeClient.mFilePathCallbacks != null) { Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData(); if (result != null) { String path = MediaUtility.getPath(getApplicationContext(), result); Uri uri = Uri.fromFile(new File(path)); mOpenFileWebChromeClient.mFilePathCallbacks .onReceiveValue(new Uri[] { uri }); } else { mOpenFileWebChromeClient.mFilePathCallbacks .onReceiveValue(null); } } mOpenFileWebChromeClient.mFilePathCallback = null; mOpenFileWebChromeClient.mFilePathCallbacks = null; } }
If there is a problem in subsequent use, add again