最近的工作项目,需要使用cordova插件开发,详细Cordova角色,不会走,你可以去百度自身OK该,直接启动。详细过程,我有一个小Demo解说提前进行。

还只是接触,东西太理论基础,我也不太清楚,或启动和上升Demo,去的动力~大家多多不吝赐教~

Step1.准备工作:

首先将我提供的Demo实例包中的HelloWorld-CordovaLib引入到工作空间中,我是使用的Eclipse。接着创建projectMultiImageChooser,同一时候将HelloWorld-CordovaLib作为Library引入到MultiImageChooser中:

接着,依照Demo实例包中的文件夹结构。引入Cordova所须要的文件。完毕后的文件夹结构例如以下所看到的:

当中,res目录下另一个xml目录,记得一并拷过去哦~

截至到如今,主要的准备工作就算是完毕了。

Step2.插件的开发

插件的编写,是为了让JS能够调用我的Activity,事实上编写起来还是比較简单的。

a.在src文件夹下建立包plugins,编写插件类Plugin_intent

package plugins;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin; import android.content.Intent;
import android.util.Log;
import android.widget.Toast; import com.wenjoy.dojo.ResponseJSON;
import com.wenjoy.multiimagechooser.MainActivity; /**
* js调用java方法
*
* 必须继承CordovaPlugin CordovaPlugin里面有实现cordovaActivity的方法
* 提供startActivityForResult();
*
* 我使用的 cordova 3.3.0版本号
*
* @author XueQi
*
*/
public class Plugin_intent extends CordovaPlugin {
private String infos; /**
* 注意 构造方法不能为
*
* Plugin_intent(){}
*
* 能够不写或者 定义为例如以下
*
*/
public Plugin_intent() {
} CallbackContext callbackContext; @Override
public boolean execute(String action, org.json.JSONArray args,
CallbackContext callbackContext) throws org.json.JSONException {
this.callbackContext = callbackContext;
Log.i("123", action); if (action.equals("intent")) {
// 获取JS传递的args的第一个參数
infos = args.getString(0);
this.function();
return true;
}
return false; } // 方法运行体
private void function() {
// cordova.getActivity() 获取当前activity的this
Log.i("123", cordova.getActivity().toString());
Intent intent = new Intent(cordova.getActivity(), MainActivity.class);
intent.putExtra("infos", infos);
cordova.startActivityForResult((CordovaPlugin) this, intent, 200); } @Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent);
// 传递返回值 给js方法
callbackContext.success(com.alibaba.fastjson.JSONArray
.toJSONString(ResponseJSON.getInstance().getJsonObjects()));
if (ResponseJSON.getInstance().getJsonObjects() != null
&& ResponseJSON.getInstance().getJsonObjects().size() > 0) {
Toast.makeText(cordova.getActivity(), "恭喜,上传完毕", 1000).show();
}
} }

b.方法编写完毕后。要在res/xml/config.xml下注冊,写在widget标签中加入

 <feature name="Demo">
<param name="android-package" value="plugins.Plugin_intent" /><!-- value:包名.类名 -->
</feature>

feature的name非常重要。一会在JS中要用到。

c.编写插件JS文件,在assert/www/plugins下,创建intent.js文件

cordova.define("org.apache.cordova.intent", function(require, exports, module) { /*
*
* 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.
*
*/ var exec = require('cordova/exec'); /**
* Provides access to the vibration mechanism on the device.
*/ module.exports = { /**
* 一共5个參数
第一个 :成功会掉
第二个 :失败回调
第三个 :将要调用的类的配置名字(在config.xml中配置 稍后在以下会解说)
第四个 :调用的方法名(一个类里可能有多个方法 靠这个參数区分)
第五个 :传递的參数 以json的格式
*/
demo: function(mills) {
exec(function(winParam){
alert(winParam);<span style="font-family: Arial, Helvetica, sans-serif;">//运行成功。winParam是类中callbackContext.success传递的參数</span>
}, null, "Demo", "intent", [mills]);
},
}; });

demo:定义被JS调用的方法名

Demo:就是我们刚才在config.xml中配置的插件类的名字

mills:这里我始终仅仅能传递一个參数,所以。我如今的解决方案是拼接一个字符串,比如:'aaa,nnn,ccc',用逗号切割三个參数

Step3.使用插件

截止到如今,整个插件就OK啦,能够创建一个html和Activiry了。这里我仅仅列出LUNCH Activity的编写和html页面

在assert/www下建立index.html文件,非常easy

<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
// 跳转
function intent() { navigator.intent.demo('NDljY2E1ZGM4NzUzM2U3Yg==,order,5740'); } //token,eneityname,entityid </script>
</head>
<body>
<p><a href="#" onclick="intent(); return false;">Upload Image</a></p>
</body>
</html>

ViewActivity:

package com.wenjoy.multiimagechooser;

import org.apache.cordova.CordovaActivity;

import android.content.Intent;
import android.os.Bundle; /**
* 装载HTML页面的Activity
*
* @author XueQi
*
*/
public class ViewActivity extends CordovaActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
// Set by <content src="index.html" /> in config.xml
super.loadUrl("file:///android_asset/www/index.html");
// super.loadUrl("file:///android_asset/www/index.html")
} @Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent); }
}

最后,至于权限什么的,大家就自己加入好了。

大工告成!附上DEMO的下载地址,要1个积分,大家不要吐槽~ http://download.csdn.net/detail/xq328220454/7620119

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Android基于cordova3.3插件开发的更多相关文章

  1. Android基于XMPP的即时通讯3-表情发送

    这篇博文主要讲表情发送的一些东西. 参考:Android基于XMPP的即时通讯1-基本对话 1.准备好资源文件 采用的是emoji的表情,我打包好了,下载地址:http://files.cnblogs ...

  2. Android基于XMPP的即时通讯2-文件传输

    本文是在上一篇博文Android基于XMPP的即时通讯1-基本对话的基础上,添加新的功能,文件传输 1.初始化文件传输管理类 public static FileTransferManager get ...

  3. Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.net/xiaanming/article/detail ...

  4. Android 基于Socket的聊天应用(二)

    很久没写BLOG了,之前在写Android聊天室的时候答应过要写一个客户(好友)之间的聊天demo,Android 基于Socket的聊天室已经实现了通过Socket广播形式的通信功能. 以下是我写的 ...

  5. Android基于XMPP Smack openfire 开发的聊天室

    Android基于XMPP Smack openfire 开发的聊天室(一)[会议服务.聊天室列表.加入] http://blog.csdn.net/lnb333666/article/details ...

  6. 【转】Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果--不错

    原文网址:http://blog.csdn.net/xiaanming/article/details/10163203 转载请注明出处:http://blog.csdn.net/xiaanming/ ...

  7. Android 基于Netty的消息推送方案之对象的传递(四)

    在上一篇文章中<Android 基于Netty的消息推送方案之字符串的接收和发送(三)>我们介绍了Netty的字符串传递,我们知道了Netty的消息传递都是基于流,通过ChannelBuf ...

  8. Android 基于Netty的消息推送方案之字符串的接收和发送(三)

    在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...

  9. Android 基于Netty的消息推送方案之概念和工作原理(二)

    上一篇文章中我讲述了关于消息推送的方案以及一个基于Netty实现的一个简单的Hello World,为了更好的理解Hello World中的代码,今天我来讲解一下关于Netty中一些概念和工作原理的内 ...

随机推荐

  1. c# 文件/文件夹操作

    1.判断文件夹是否存在并创建 if (!Directory.Exists(tempFolderName)) { Directory.CreateDirectory(tempFolderName); }

  2. 【Unity3D自学记录】Unity3D网络之Socket聊天室初探

    首先创建一个服务端程序,这个程序就用VS的控制台程序做即可了. 代码例如以下: using System; using System.Collections.Generic; using System ...

  3. 积累的VC编程小技巧之工具条和状态条

    1.工具条和状态条中控件的添加: 方法⑴.只能在ToolBar里创建控件:首先,在ToolBar中创建一个Button,其ID为ID_TOOL_COMBO(我们要将创建的控件放在该Button的位置上 ...

  4. must return an Iterable of arrays.(junit4)

    java.lang.Exception: TestIterator.init() must return an Iterable of arrays. at org.junit.runners.Par ...

  5. Win32环境下的程序崩溃异常定位

    1       案例描述 作为Windows程序员,平时最担心见到的事情可能就是程序发生了崩溃(异常),这时Windows会提示该程序执行了非法操作,即将关闭.请与您的供应商联系.呵呵,这句微软的“名 ...

  6. Tomcat 配置WEB虚拟映射 及 配置虚拟主机

    Tomcat  配置WEB虚拟映射 及 配置虚拟主机 配置WEB虚拟映射文件夹有三种方法例如以下: 第一(要重新启动server的): 打开路径 Tomcat 6.0\conf 下的 server.x ...

  7. struts 2吊牌s:if 、s:iterator注意

    疏忽,也没有相应的总结.实际上JSTL标签Struts2标签混淆.导致一些上述问题的细节.今天我给从下一个总结,同 后不要再犯这种错误. 总喜欢在s:if标签里面使用$,导致各种数据读不出来. str ...

  8. Linux入门基础 #9:管道及重定向

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  9. Qt中提高sqlite的读写速度(使用事务一次性写入100万条数据)

    SQLite数据库本质上来讲就是一个磁盘上的文件,所以一切的数据库操作其实都会转化为对文件的操作,而频繁的文件操作将会是一个很好时的过程,会极大地影响数据库存取的速度.例如:向数据库中插入100万条数 ...

  10. python大文件迭代器的流式读取,之前一直使用readlines()对于大文件可以迅速充满内存,之前用法太野蛮暴力,要使用xreadlines或是直接是f,

    #!/usr/bin/env python #encoding=utf-8 import codecs count =0L #for line in file("./search_click ...