Android基于cordova3.3插件开发
最近的工作项目,需要使用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插件开发的更多相关文章
- Android基于XMPP的即时通讯3-表情发送
这篇博文主要讲表情发送的一些东西. 参考:Android基于XMPP的即时通讯1-基本对话 1.准备好资源文件 采用的是emoji的表情,我打包好了,下载地址:http://files.cnblogs ...
- Android基于XMPP的即时通讯2-文件传输
本文是在上一篇博文Android基于XMPP的即时通讯1-基本对话的基础上,添加新的功能,文件传输 1.初始化文件传输管理类 public static FileTransferManager get ...
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.net/xiaanming/article/detail ...
- Android 基于Socket的聊天应用(二)
很久没写BLOG了,之前在写Android聊天室的时候答应过要写一个客户(好友)之间的聊天demo,Android 基于Socket的聊天室已经实现了通过Socket广播形式的通信功能. 以下是我写的 ...
- Android基于XMPP Smack openfire 开发的聊天室
Android基于XMPP Smack openfire 开发的聊天室(一)[会议服务.聊天室列表.加入] http://blog.csdn.net/lnb333666/article/details ...
- 【转】Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果--不错
原文网址:http://blog.csdn.net/xiaanming/article/details/10163203 转载请注明出处:http://blog.csdn.net/xiaanming/ ...
- Android 基于Netty的消息推送方案之对象的传递(四)
在上一篇文章中<Android 基于Netty的消息推送方案之字符串的接收和发送(三)>我们介绍了Netty的字符串传递,我们知道了Netty的消息传递都是基于流,通过ChannelBuf ...
- Android 基于Netty的消息推送方案之字符串的接收和发送(三)
在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...
- Android 基于Netty的消息推送方案之概念和工作原理(二)
上一篇文章中我讲述了关于消息推送的方案以及一个基于Netty实现的一个简单的Hello World,为了更好的理解Hello World中的代码,今天我来讲解一下关于Netty中一些概念和工作原理的内 ...
随机推荐
- [置顶] PMBOOK第四版-ITO与数据流图总结
具体文档下载地址: 点击打开文档下载地址 :http://download.csdn.net/detail/lyjluandy/6694205 一.过程组与知识领域表(简图) 二.输入 - 工具 - ...
- 页面导出生成pdf,使用wkhtmltopdf第三方工具
把页面导出生成pdf,这里用到第三方的工具,使用方法中文文档没有找到,网上也没找到网友详细的神作.没有深入研究,所以也不赘述了,当然最基本的使用大多数也够用了,详细参数的官网也没介绍,大家使用的时候, ...
- VIM 用正则表达式
VIM 用正则表达式 批量替换文本,多行删除,复制,移动 在VIM中 用正则表达式 批量替换文本,多行删除,复制,移动 :n1,n2 m n3 移动n1-n2行(包括n1,n2)到n3行之下: ...
- 基础知识(9)- Swing用户界面组件
9.1 Swing和模型-视图-控制器设计模式 9.1.1 设计模式 9.1.2 模型-视图-控制器模式 9.1.3 Swing按钮的模型-视图-控制器分析 9.2 布局管理概述 9.2.1 ...
- Cookie不能保存中文解决方式
在用cookie保存username的时候,发现cookie值不能存中文,报例如以下错: Control character in cookie value, consider BASE64 e ...
- BDIA增强
SE24 CL_EXITHANDLER的方法GET_INSTANCE中有基本上所有的增强都会走这边,打上断点查找增强名称,或者在程序中全局搜索GET_INSTANCE关键字 然后 SE19 下 ...
- 海蜘蛛网络科技官方网站 :: 做最好的中文软路由 :: 软件路由器 :: 软路由 :: 软件路由 :: RouterOs
海蜘蛛网络科技官方网站 :: 做最好的中文软路由 :: 软件路由器 :: 软路由 :: 软件路由 :: RouterOs 企业简介 武汉海蜘蛛网络科技有限公司成立于2005年,是一家专注于网络新技术研 ...
- Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站
Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站 Mysql 官方Memcached 插件初步试用感受
- 450A - Jzzhu and Children 找规律也能够模拟
挺水的一道题.规律性非常强,在数组中找出最大的数max,用max/m计算出倍数t,然后再把数组中的书都减去t*m,之后就把数组从后遍历找出第一个大于零的即可了 #include<iostream ...
- 解决xShell4某些情况下按删除键会输出^H的问题
当我们用Xshell登录进入linux后,在普通模式下,对输入进行删除等操作没有问题. 而在执行中,按delete,backspace键时会产生^H等乱码问题. 这是由于编码不匹配的问题. 解决方法: ...