Implementation of Android plug-in based on PhoneGap2.9 framework

Source: Internet
Author: User



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!




Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.