Zookeeper
The PhoneGap platform provides plug-in functions. developers can encapsulate heavyweight functions in native code development plug-ins and expose interfaces to JavaScript, javaScript calls the plug-in function to complete interaction with local code. The development of a simple android plug-in involves the following steps:
(1) Write a JAVA class that inherits the CordovaPlugin class as follows:
Import org. apache. cordova. api. CallbackContext; Import org. apache. cordova. api. CordovaPlugin; Import org. json. JSONArray; Import org. json. JSONException; Public class Login extends CordovaPlugin { Public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { If (action. equals ("login ")){ String message = args. getString (0 ); This. login (message, callbackContext ); Return true; } Return false; } Private void login (String message, CallbackContext callbackContext ){ If (message! = Null & message. length ()> 0 ){ CallbackContext. success ("loginSuccess "); } Else { CallbackContext. error ("Expected one non-empty string argument ."); } } }
|
(2) register the plug-in config. xml as follows:
(3) js call:
<Script type = "text/javascript" charset = "UTF-8"> Function login (str, callback ){ Var exec = cordova. require ('cordova/exec '); Exec (callback, function (err ){ Callback ('Nothing to echo .'); }, "Login", "login", [str]); }; Function success (result ){ Alert ("SUCCESS: \ r \ n" + result ); } Login ("para", success ); </Script> |
(4) print loginSuccess!
Zookeeper