插件开发流程:
1 定义插件类(extends CordovaPlugin)
<pre name="code" class="java">
package com.example.mycordovatest;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.LOG;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
/**
* This class provides a service.
*/
public class ActivityPlugin extends CordovaPlugin {
static String TAG = "ActivityPlugin";
/**
* Constructor.
*/
public ActivityPlugin() {
}
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/
public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) {
PluginResult result = new PluginResult(PluginResult.Status.OK, "");
try {
if (action.equals("start")) {
this.startActivity(args.getString(0));
callbackContext.sendPluginResult(result);
callbackContext.success();
return true;
}
} catch (JSONException e) {
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, "JSON Exception");
callbackContext.sendPluginResult(result);
return false;
}
return false;
}
// --------------------------------------------------------------------------
// LOCAL METHODS
// --------------------------------------------------------------------------
public void startActivity(String className) {
try {
Intent intent = new Intent().setClass(this.cordova.getActivity(), Class.forName(className));
LOG.d(TAG, "Starting activity %s", className);
this.cordova.getActivity().startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
LOG.e(TAG, "Error starting activity %s", className);
}
}
}
2 res/xml/config.xml配置文件中配置该插件
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<widget id="io.cordova.helloCordova" version="2.0.0" xmlns="http://www.w3.org/ns/widgets">
<name>Hello Cordova</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<access origin="*.apache.org" />
<access origin="http://*.google.com/*" />
<access origin="https://*.google.com/*" />
<access origin="https://*.googleapis.com/*" />
<access origin="https://*.gstatic.com/*" />
<content src="index.html" />
<preference name="loglevel" value="DEBUG" />
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="showTitle" value="true" />
<feature name="Activity">
<param name="android-package" value="com.example.mycordovatest.ActivityPlugin" />
</feature>
</widget>
3 写js脚本调用插件
</pre><pre name="code" class="html"><!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320, user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Cordova Tests</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title">
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>
<script>
function startActivity(className) {
cordova.exec(function() {console.log("Success");}, function(e) {console.log("Error: "+e);}, "Activity", "start", [className]);
};
function startActivity2(className){
navigator.intent.demo(className);
};
localStorage.lifecyclestatus = "";
localStorage.backgroundstatus = "";
</script>
</head>
<body οnlοad="init();" id="stage" class="theme">
<h1>Cordova Android Native Tests</h1>
<div id="info">
<h4>Cordova: <span id="cordova"> </span></h4>
<h4>Deviceready: <span id="deviceready"> </span></h4>
</div>
<div id="info">
<h4>Run each of the test activities below:</h4>
</div>
<button class="btn large" οnclick="startActivity('com.example.mycordovatest.TestActivity');">go to testActivity</button>
<button class="btn large" οnclick="startActivity2('com.example.mycordovatest.TestActivity');">go to testActivity</button>
</body>
</html>
js调用自定义插件的原理其实就是调用cordova.js中的
/**
* 一共5个参数
第一个 :成功会掉
第二个 :失败回调
第三个 :将要调用的类的配置名字(在config.xml中配置 稍后在下面会讲解)
第四个 :调用的方法名(一个类里可能有多个方法 靠这个参数区分)
第五个 :传递的参数 以json的格式
*/
cordova.exec(successcallback,failcallback,pluginname,pluginmethod,arg)方法
有时候我们会把这个调用代码放到一个单独的js文件(intent.js)中:
cordova.define("org.apache.cordova.intent", function(require, exports, module) {
module.exports = {
demo: function(className) {
cordova.exec(function() {console.log("Success");}, function(e) {console.log("Error: "+e);}, "Activity", "start", [className]);
},
};
});
然后在cordova_plugins.js中定义
module.exports = [
{
"file": "plugins/intent.js",
"id": "org.apache.cordova.intent",
"merges": [
"navigator.intent"
]
},
];
然后通过navigator.intent.demo(activityname)调用