PhoneGap Android next to write PhoneGap plugin

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.