码迷,mamicode.com
首页 > 移动开发 > 详细

cordova android项目自定义插件及使用(一)

时间:2015-11-09 18:50:36      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

 
--新建cordova项目
  cordova create Myproj com.myproj.test MyTestProj
--添加平台及设备
  cd Myproj
  cordova platform add android
  cordova platform add ios
  cordova plugin add org.apache.cordova.device
--然后这个时候打开项目目录是这个样子的
  技术分享
  config:应用的配置信息
  platforms:应用支持的平台目录
  plugins:安装的插件目录
  www:web工程目录
  我们开发的插件最终要添加到plugins目录当中
 
  添加自定义插件,创建ExtendInfo文件夹,结构如下:
  ExtendInfo
  |—src
  |      |—android
  |                 |—ExtendInfo.java
  |      |—ios
  |—www
  |          |—ExtendInfo.js
  |—plugin.xml
 
    src:对应不同平台
    www:放我们的javascript
    plugin.xml:插件配置文件
 
  这里是plugin.xml
 
<?xml version="1.0" encoding="utf-8"?>
<plugin id="com.myproj.test" version="0.0.1"
    xmlns="http://apache.org/cordova/ns/plugins/1.0"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <name>ExtendInfo</name>
    <description>Description</description>
    <js-module name="ExtraInfo" src="www/ExtendInfo.js">
        <clobbers target="cordova.plugins.ExtendInfo"/>
    </js-module>
    <platform name="android">
        <config-file parent="/*" target="res/xml/config.xml">
            <feature name="ExtendInfo">
                <param name="android-package" value="com.myproj.test.ExtraInfo"/>
            </feature>
        </config-file>
        <source-file src="src/android/ExtendInfo.java" target-dir="src/com/myproj/test"/>
    </platform>
</plugin>

  其中:

    id:插件的标识,即发布到plugins.cordova.io的id
    name:插件名称
    description:描述
    js-module:对应我们的javascript文件,src属性指向:www/ExtendInfo.js
    platform:支持平台。这里仅有android
 
<config-file parent="/*" target="res/xml/config.xml">
            <feature name="ExtendInfo">
                <param name="android-package" value="com.myproj.test.ExtraInfo"/>
            </feature>
        </config-file>
        <source-file src="src/android/ExtendInfo.java" target-dir="src/com/myproj/test"/>

 

  这是插件的配置信息,最后会添加到android项目的 res/xml/config.xml文件中,并且将我们的src/android/下的ExtendInfo.java复制到android
  项目的package包当中去
 
  ExtendInfo.js内容如下:
var exec = require(‘cordova/exec‘);

exports.getExtra = function(success, error) {
    exec(success, error, "ExtendInfo", "getExtra", []);
};

  ExtendInfo.java内容如下:

 
public class ExtendInfo extends CordovaPlugin {
    
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
    throws JSONException {
        Activity activity = this.cordova.getActivity();
        if (action.equals("getExtra")) {
            Intent i = activity.getIntent();
            if (i.hasExtra(Intent.EXTRA_TEXT)) {
                callbackContext.success(i.getStringExtra(Intent.EXTRA_TEXT));
            } else {
                callbackContext.error("");
            }
            return true;
        }
        return false;
    }
}

 

  到这里,我们的插件就编写Ok了。通过如下命令添加插件:
    cordova plugin add ExtendInfo
  这样子我们的插件就添加到项目啦
 
--使用eclipse打开项目,我们会发现assets下的目录不显示
  技术分享
  我们点击打开这个txt文件,按步骤操作就可以显示啦,并且res/config.xml也可以显示了。
  此时config.xml当中已经添加了我们自定义的插件信息,如下:
  技术分享
  当中的content src=“index.html”指向的就是assets/www/index.html
 
   点击就可以运行了,具体自定义插件应用看下一篇。
 
 

cordova android项目自定义插件及使用(一)

标签:

原文地址:http://www.cnblogs.com/rebeccay/p/4950614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!