JS calls Java methods through PhoneGap and passes the arguments to each other.
One, Java code
Write a class that inherits from plugin and overrides the Execute method.
ImportOrg.json.JSONArray;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;Importandroid.content.ActivityNotFoundException;ImportAndroid.content.DialogInterface;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importcom.phonegap.api.PhonegapActivity;ImportCom.phonegap.api.Plugin;ImportCom.phonegap.api.PluginResult; Public classPlugintestextendsPlugin {
Public static String ACTION = "Hello";
Publicplugintest () {}/*** Executes the request and returns Pluginresult. * * @paramaction the action to execute. * @paramargs Jsonarray of arguments for the plugin. * @paramCallbackid The callback ID used when calling back to JavaScript. * @returna Pluginresult object with a status and message. */@Override PublicPluginresult Execute (String action, Jsonarray args, string callbackid) {Try{jsonobject jsonobj=NewJsonobject ();//JSON data that can be returned to JS if(Action.equals ("Hello") ) {String str1= args.getstring (0);//get the first parameterString str2= args.getstring (1);//get the second argumentJsonobj.put ("str1", str1+ "1");//Put the parameters in the Jsonobject objectJsonobj.put ("str2", str2+ "2");//Put the parameters in the Jsonobject object} pluginresult R=NewPluginresult (pluginresult.status.ok,jsonobj); returnR; } Catch(Exception e) {e.printstacktrace (); } }}
Second, configure the plug-in in Plugins.xml
Adding configuration information to the new plug-in in the Plugins.xml file
<?XML version= "1.0" encoding= "Utf-8"?><Plugins> <pluginname= "APP"value= "COM.PHONEGAP.APP"/> <pluginname= "Geolocation"value= "Com.phonegap.GeoBroker"/> <pluginname= "Device"value= "Com.phonegap.Device"/> <pluginname= "Accelerometer"value= "Com.phonegap.AccelListener"/> <pluginname= "Compass"value= "Com.phonegap.CompassListener"/> <pluginname= "Media"value= "Com.phonegap.AudioHandler"/> <pluginname= "Camera"value= "Com.phonegap.CameraLauncher"/> <pluginname= "Contacts"value= "Com.phonegap.ContactManager"/> <pluginname= "Crypto"value= "Com.phonegap.CryptoHandler"/> <pluginname= "File"value= "Com.phonegap.FileUtils"/> <pluginname= "Network Status"value= "Com.phonegap.NetworkManager"/> <pluginname= "Notification"value= "Com.phonegap.Notification"/> <pluginname= "Storage"value= "Com.phonegap.Storage"/> <pluginname= "Temperature"value= "Com.phonegap.TempListener"/> <pluginname= "Filetransfer"value= "Com.phonegap.FileTransfer"/> <pluginname= "Capture"value= "Com.phonegap.Capture"/> <!--new Plug-in class configuration name write your class name, value write package name. Class Name - <pluginname= "Plugintest"value= "com.easyway.barcode.PluginTest "/> </Plugins>
Third, the JavaScript file registration plugin
Create a new. js file and put the file in the same directory as the PhoneGap file. (Create a new simpleplugin.js file)
varSimpleplugin =function() {};
//str1 and str2 are parameters that are passed to Java SimplePlugin.prototype.hello=function(Successcallback, Failurecallback, str1, str2) {//the parameters of the exec are: Success Callback, Failure Callback, registered Plugin name: The name that corresponds to the XML file configuration , //' Hello ' is a parameter in the Execute method of the incoming Java file string action //name (parameters from HTML) returnPhonegap.exec (Successcallback, Failurecallback, ' plugintest ', ' Hello ', [STR1,STR2]);};//here is PhoneGap Plugin's registration, Plugin's name and Native Class's name is not wrong, is that we have just entered thePhonegap.addconstructor (function() { //Register the JavaScript plugin with PhoneGapPhonegap.addplugin (' Simpleplugin ',NewSimpleplugin ());//Simpleplugin is the plug-in name, and new Simpleplugin () instantiates the class name of this JavaScript});
Iv. calling methods in an HTML file
Introduction of PhoneGap and plug-in JS files in HTML files, calling methods
<! DOCTYPE html><HTML> <Head> <MetaCharSet= "Utf-8"> <title>Java Pass-through parameters</title> <Scriptsrc= "Phonegap.js"></Script> <!--PhoneGap Bag - <Scriptsrc= "Js/jquery.js"></Script> <Scriptsrc= "Simpleplugin.js"></Script><!--custom plug-in Files - <Script>$ (document). Ready (function(e) {$ ("#btn_test"). Click (function() {Window.plugins.simplePlugin.hello (function(Result) {alert ("The first parameter returned:"+RESULT.STR1+"The second parameter returned"+result.str2); }, function(Error) {},"first parameter", "a second parameter" ); }); }); </Script> </Head><Body><Buttontype= "button"ID= "Btn_test">Click me!</Button></Body></HTML>
JS calls Java methods and passes arguments to each other