How to write an Android plugin for PhoneGap?
The other way around is to write a PhoneGap plugin for Android.
Here is a simple Hello World! As an example, to illustrate:
1. Build an Android project that supports PhoneGap (Cordova)
Since the plugin is essentially an Android plugin for PhoneGap, we will build an Android project to support PhoneGap (Cordova). The plugin will be written in this project.
Literacy: PhoneGap has now been sold to the Apache Foundation and is now called Cordova. The name comes from the fact that the company that created PhoneGap was located on a street called Cordova, presumably in memory. To download the latest version of Cordova, go to the PhoneGap website, which is stuck at 2.9.1.Copy the code
Cordova CLI is used for project creation. It would be more convenient. Otherwise, you have to create an Android project, manually copy the Cordova to the project folder, and modify the code.
\
1) Create a Cordova project first
In command line mode:
cordova create hello com.example.test HelloWorld
Copy the code
This creates a folder named Hello in the current path, which contains the various files for Cordova
2) Set Cordova to support Android
On the command line: \
cd hello
cordova platform add android
Copy the code
So hello \ platforms folder, one more android subdirectories, this is the support phoegap android project, plug-in can be written in the project.
2. Write plug-ins
1) Hello. Java
Open the project \ with Eclipse
Create a new package and class under SRC
Hello. Java:
package com.example.test.plugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
public class Hello extends CordovaPlugin{
@Override
public boolean execute(String action , JSONArray args , CallbackContext callbackContext) throws JSONException {
try{
if (action.equals("sayHello")) {
callbackContext.success("Hello World! Hello, Cordova!");
return true; }}catch (Exception e) {
callbackContext.error("Oh shit!");
return false;
}
return super.execute(action, args, callbackContext); }}Copy the code
\
2) config. XML
Modify the res/XML/config. XML
In the node <widget> add:
<feature name="Hello">
<param name="android-package" value="com.example.test.plugin.Hello" />
</feature>
Copy the code
\
3. Call the plug-in
Invocation is javascript’s one-man show.
1) Add js/plugin.js
\
var helloPlugin = {
say: function(successCallback, errorCallback) {
cordova.exec(
successCallback, // success callback function
errorCallback, // error callback function
'Hello'.// mapped to our native Java class called "CalendarPlugin"
'sayHello'.// with this action name
[] // and this array of custom arguments to create our entry); }}Copy the code
\
2) Modify js/index.js
* * * *
The revision is mainly at the end
\
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready'.this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...) ; '
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style'.'display:none; ');
receivedElement.setAttribute('style'.'display:block; ');
console.log('Received Event: ' + id);
app.sayHello();
},
sayHello: function(){
var success = function(message) { alert(message); };
var error = function(message) { alert("Oopsie! "+ message); }; helloPlugin.say(success, error); }};Copy the code
\
3) Modify the home page index.html
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/plugin.js"></script>
<script type="text/javascript">
app.initialize();
</script>
Copy the code
This completes the process of writing and calling an Android plug-in for PhoneGap.
Plug-ins are supposed to be independent of the project and can be invoked by multiple projects, but this example is written directly within the project. Independent way, I don’t know how to write it yet.
\
\
\