First, the JavaScript side of the writing
The first parameter succeeds in a callback function
The second parameter fails the callback function
The third parameter is the class name of the plug-in, which is the class name of the background Java file
The fourth parameter executes the action name corresponding to the action name in the background Java code
The argument passed by the fifth argument is an array
function HelloWorld (success,error,option) {cordova.exec (Success, error, "HelloWorld", "echo", option),//option can also use [ XXX,XXX], he is an array}
$ (document). Ready (function() {Document.addeventlistener ("Deviceready", Ondeviceready,false); });functionOndeviceready () {HelloWorld (function(SUCC) {alert (' Success ' +succ); },function(ERR) {alert (' Failure: ' +err); },[' Hellomobileworld ', ' Testworld ']);} functionHelloWorld (success,error,option) {cordova.exec (Success, error,"HelloWorld", "Echo1", option);}
Second, register the plugin
Register under the config file in the android:res/xml/directory
<name= "Helloword"><name= " Android-package " value=" Org.apache.cordova.helloword.HelloWord "/> </feature>
third, set permissions (need to invoke permissions to set permissions where)
<android:name= "Android.permission.SEND_SMS"/>
Iv. Java Code Writing
1. Create a package column such as:Com.example.phonegap.plugin
2. Create a class column in the package such as: HelloWorld
PackageCom.example.phonegap.plugin;ImportOrg.apache.cordova.api.CallbackContext;ImportOrg.apache.cordova.api.CordovaPlugin;ImportOrg.apache.cordova.api.PluginResult;ImportOrg.json.JSONArray;Importorg.json.JSONException;ImportOrg.json.JSONObject; Public classHelloWorldextendsCordovaplugin {@Override Public BooleanExecute (String action, Jsonarray args, Callbackcontext callbackcontext)throwsjsonexception {if(Action.equals ("echo")) {//Action=echo Here corresponds to the JavaScript side of the fourth parameter, if it is not the same as the parameters inside will not execute the following codeString message = args.getstring (0);//Gets the element in the fifth parameter arrayHellomobileworld
This. Echo (message, callbackcontext); return true; }Else{callbackcontext.error ("This is not an echo operation"); return false; } } Private voidEcho (String message, Callbackcontext callbackcontext) {if(Message! =NULL&& message.length () > 0) {callbackcontext.success (message); } Else{callbackcontext.error ("Expected one non-empty string argument."); } }}
PhoneGap Android next to write PhoneGap plugin