Js calls Java methods through PhoneGap and passes parameters to each other.
1. JAVA code
Write a class that inherits from Plugin and overrides the execute method.
import org.json.JSONArray;import android.app.Activity;import android.app.AlertDialog;import android.content.ActivityNotFoundException;import android.content.DialogInterface;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import com.phonegap.api.PhonegapActivity;import com.phonegap.api.Plugin;import com.phonegap.api.PluginResult;public class PluginTest extends Plugin { public static String ACTION = "hello"; public PluginTest() { } /** * Executes the request and returns PluginResult. * * @param action The action to execute. * @param args JSONArray of arguments for the plugin. * @param callbackId The callback id used when calling back into JavaScript. * @return A PluginResult object with a status and message. */ @Override public PluginResult execute(String action, JSONArray args, String callbackId) { try { JSONObject jsonObj = new JSONObject();//The JSON data that can be returned to JS if (action.equals("hello")) { String str1= args.getString(0); //Get the first parameter String str2= args.getString(1); //Get the second parameter jsonObj.put("str1", str1+"1"); //Put the parameter into the JSONObject object jsonObj.put("str2", str2+"2"); //Put the parameters into the JSONObject object} PluginResult r = new PluginResult(PluginResult.Status.OK,jsonObj); return r; } catch (Exception e) { e.printStackTrace(); } }}3. Register plug-in in Javascript file
Create a new .js file and place the file and phonegap file in the same directory. (Create a new simplePlugin.js file)
var SimplePlugin = function() {};//str1 and str2 are the parameters passed to JAVA SimplePlugin.prototype.hello = function(successCallback, failureCallback, str1, str2) { // The exec score is: Success Callback, Failure Callback, Registered Plugin name: It is the corresponding name configured in the XML file, // 'hello' is the parameter String action in the execute method passed into the Java file // name (HTML) return PhoneGap.exec(successCallback, failureCallback, 'PluginTest', 'hello', [str1,str2]);};// This is the PhoneGap Plugin. The Plugin name is the name of Native Class, which is the PhoneGap.addConstructor(function() { // Register the javascript plugin with PhoneGap PhoneGap.addPlugin('simpleplugin', new SimplePlugin()); //simpleplugin is the plugin name, and new SimplePlugin() instantiates the class name of this Javascript});4. Calling methods in HTML files
Introduce phonegap and plugin js files in html file, call method
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>JAVA parameter transfer</title> <script src="phonegap.js"></script> <!--phonegap package--> <script src="js/jquery.js"></script> <script src="simplePlugin.js"></script><!--Custom plugin file--> <script> $(document).ready(function(e) { $("#btn_test").click(function(){ window.plugins.simplePlugin.hello( function(result) { alert("Returned first parameter: "+result.str1+"Returned second parameter"+result.str2); }, function(error) { }, "first parameter", "second parameter" ); }); }); </script> </head><body><button type="button" id="btn_test">Click Me!</button></body></html>The above simple example of Js calling Java methods and passing them over to each other is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.