The PhoneGap platform provides plug-in functionality that enables developers to encapsulate heavyweight functionality in native code-developed plug-ins and expose interfaces to Javascript,javascript invoke plug-in functionality to interact with native code. Developing a simple Android plugin is mainly divided into the following steps:
(1) Write Java class, inherit 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 && Amp Message.length () > 0) { callbackcontext.success ("loginsuccess"); } else { callbackcontext.error ("expected one non-empty string argument."); } } } |
(2) Register the plugin in CONFIG. reg, as follows:
<feature name= "Login" > <param name= "Android-package" value= "Cn.com.vs.service.Login"/> </feature> |
(3) JS call, as follows:
<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 out loginsuccess, you can succeed!