版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010106153/article/details/53418528
Cordova plugin
工程目录

src: 各个平台的源码文件,包含 Android ios blackberry 等;
www: JavaScript调用native代码的接口文件;
plugin.xml: 插件的配置文件

编写Android代码
首先定义一个类继承自CordovaPlugin:
public class Updater extends CordovaPlugin {
public Updater(){
}
}
1
2
3
4
在该类中添加一个方法execute:
public class Updater extends CordovaPlugin {
public Updater(){

}

public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (action.equals("update")) {
new UpdaterTask(callbackContext).execute(args.getString(0));
}else{
return false;
}
return true;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
action:在JavaScript中调用时指定的action名称;
args:在JavaScript中调用时传入的参数;
callbackContext:向JavaScript返回结果的上下文对象,正确返回时callbackContext.success(data),错误返回时 callbackContext.error(err)

注意:cordova plugin中的上下文与Android native中的上下文有所区别,例如在cordova中启动一个Activity的写法是:cordova.getActivity().startActivity(Intent intent),且cordova plugin中的Activity需要继承自CordovaActivity.

编写plugin.xml文件
首先看一个例子:

<?xml version='1.0' encoding='utf-8'?>
<plugin id="expense-plugin-updater" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>ExpenseUpdater</name>
<js-module name="ExpenseUpdater" src="www/expense-plugin-updater.js"><clobbers target="expenseupdater"/></js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="ExpenseUpdater"><param name="android-package" value="expense.plugin.updater.Updater"/></feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
</config-file>
<source-file src="src/android/Updater.java" target-dir="src/expense/plugin/updater"/>
<source-file src="src/android/UpdaterTask.java" target-dir="src/expense/plugin/updater"/>
<source-file src="src/android/DecompressFast.java" target-dir="src/expense/plugin/updater"/>
</platform>
</plugin>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
js-module: name:接口文件的名字 src:接口文件的路径 clobbers.target:在宿主工程中该plugin的对象名称;
feature:每一个调用入口都需要在这里注册,即:extends CordovaPlugin的类
source-file:源码文件的map关系
plugin.xml详细的教程见:https://cordova.apache.org/docs/en/latest/plugin_ref/spec.html
编写接口文件
例子:

var exec = require('cordova/exec');

exports.update = function(url, success, error) {
exec(success, error, "ExpenseUpdater", "update", [url]);
};
1
2
3
4
5
success: 正确回调
error:错误回调
ExpenseUpdater:需要调用的feature的名称
update:需要调用的action的名称
[url]:传入的参数数组
在Ionic2中如何使用
首先将该plugin安装到ionic工程中:
cordova plugin add [path-to-your-plugin]
1
编写一个调用的interface[updater.d.ts]
export declare class ExpenseUpdater {
update(url: string): Promise<any>;
}
1
2
3
实现interface[updater.js]
"use strict";
var ExpenseUpdater = (function(){
function ExpenseUpdater(){};

ExpenseUpdater.prototype.update=function(url){
return new Promise(function(resolve,reject){
expenseupdater.update(url,function(msg){
resolve(msg);
},function(err){
reject("fuck");
});
});
};
return ExpenseUpdater;
}());

exports.ExpenseUpdater = ExpenseUpdater;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
注意:其中expenseupdater对应pluign.xml文件中的js-module标签中的clobbers.target
---------------------
作者:LehmanHe
来源:CSDN
原文:https://blog.csdn.net/u010106153/article/details/53418528
版权声明:本文为博主原创文章,转载请附上博文链接!

Cordova plugin的更多相关文章

  1. [Cordova] Plugin里使用iOS Framework

    [Cordova] Plugin里使用iOS Framework 前言 开发Cordova Plugin的时候,在Native Code里使用第三方Library,除了可以加速项目的时程.也避免了重复 ...

  2. [Cordova] Plugin开发架构

    [Cordova] Plugin开发架构 问题情景 开发Cordova Plugin的时候,侦错Native Code是一件让人困扰的事情,因为Cordova所提供的错误讯息并没有那么的完整.常常需要 ...

  3. [Cordova] Plugin里使用Android Library

    [Cordova] Plugin里使用Android Library 前言 开发Cordova Plugin的时候,在Native Code里使用第三方Library,除了可以加速项目的时程.也避免了 ...

  4. [Cordova] Plugin开发入门

    [Cordova] Plugin开发入门 Overview Cordova的设计概念,是在APP上透过Web控件来呈现Web页面,让Web开发人员可以操作熟悉的语言.工具来开发APP.使用Web页面来 ...

  5. cordova plugin数据传递概要

    cordova plugin数据传递概要: 1.调用pluginManager向所有插件发送消息: PluginManager.postMessage(String id, Object data); ...

  6. ionic cordova plugin simple demo

    要用cordova plugin 的话还是需要设置一下的 1. 下载 ng-cordova.js download the zip file here 2. 在index.html 中引用 (cord ...

  7. 在meteor中如何使用ionic组件tabs,及如何添加使用cordova plugin inappbrower

    更新框架: meteor update meteor框架的优点不言而喻,它大大减轻了App前后端开发的负担,今年5月又获得B轮2000万融资,代表了市场对它一个免费.开源开发框架的肯定.cordova ...

  8. cordova plugin汇总大全

    1.获取当前应用的版本号 cordova plugin add cordova-plugin-app-version 2.获取网络连接信息 cordova plugin add cordova-plu ...

  9. vue+cordova构建跨平台应用集成并使用Cordova plugin

    安装 //安装 vue-cil npm install --global vue-cli //安装cordova npm i cordova -g cordova 新建项目 //新建cordova 项 ...

随机推荐

  1. VS2017搭建驱动开发环境WDK

    先安装VS2017,然后在安装WDK,WDK会自动关联到VS2017中,不用你任何操作,自动在新建项目中可以找到驱动开发. 如果以上安装完成后,在VS2017中新建项目中没有发现WDK,那么需要进行修 ...

  2. C#运算符的简单使用测试

    在代码中看到的代码中|=,有点不太理解故重新学习了下位运算符. 位运算符在 c# 中的测试用例 [TestMethod] public void TestMethod1() { var a = fal ...

  3. Go基础(1)

    demo1: package add var Name string = "hello world" var Age int = 10 package main import ( ...

  4. asp.net core导入excel

    接昨天的导出 导入excel内容 对比昨天导出的内容增加了一行实体属性名称作为标题行,这样到转换为实体的时候才能找到对应的属性. 导入代码 public IActionResult InportExc ...

  5. (转)学习MySQL优化原理,这一篇就够了!

    原文:https://mp.weixin.qq.com/s__biz=MzI4NTA1MDEwNg==&mid=2650763421&idx=1&sn=2515421f09c1 ...

  6. springboot~mogodb多条件拼接

    起因 当前我们使用mongodb进行查询时,有时我们的条件是分块生成的,它可能来自一个列表里,我们的条件需要根据列表去返回数据,这里有个问题,如果遍历列表,然后每次都去从mongodb里查询数据 ,这 ...

  7. Matlab图像处理常用基本函数

    之前用Matlab做图像处理工作时,用到什么函数就查什么函数,从没做过系统的总结,再做的时候又要去查,所以总结还是有必要的~ 为了方便,在此只列出函数名和基本用法,如不特别指出,不详细说明参数,辅助h ...

  8. SpringBoot技术栈搭建个人博客【前台开发/项目总结】

    前言:写前台真的是我不擅长的东西...所以学习和写了很久很久...前台页面大概开发了两天半就开发好了,采用的静态的html和bootstrap来写,写后台的时候纠结住了...怎么说呢,写页面真的是头疼 ...

  9. 3.1依赖注入「深入浅出ASP.NET Core系列」

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. 从UML来理解依赖 1.1什么是依赖 我们先看下图 可以简单理解,一个HomeController类使用到了DBC ...

  10. DSAPI 字符串和文件转Md5字符串

    方法列表: 字符串转MD5字符串(ByVal 要转换的字符串 As String, Optional 转换格式 As MD5格式 = MD5格式.小写32位) As String 文件转MD5字符串( ...