Android基于cordova3.3的插件开发

Android基于cordova3.3的插件开发


  最近工作的项目,需要用到cordova进行插件开发,具体Cordova的作用,就不再赘述,大家可以自行的去百度就OK了,直接开始。具体的流程,我将已一个小的Demo进行推进讲解。也是刚刚接触,太理论的基础东西我也说不好,还是先跑起来一个Demo,才有继续学下去的动力~大家多多指教~

  Step1.准备工作:

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

         

    接着,按照Demo实例包中的目录结构,引入Cordova所需要的文件,完成后的目录结构如下所示:

                

    其中,res文件夹下还有一个xml文件夹,记得一并拷过去哦~

    截至到现在,基本的准备工作就算是完成了。


Step2.插件的开发

    插件的编写,是为了让JS可以调用我的Activity,其实编写起来还是比较简单的。

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

    

  1. package plugins;  
  2.   
  3. import org.apache.cordova.CallbackContext;  
  4. import org.apache.cordova.CordovaPlugin;  
  5.   
  6. import android.content.Intent;  
  7. import android.util.Log;  
  8. import android.widget.Toast;  
  9.   
  10. import com.wenjoy.dojo.ResponseJSON;  
  11. import com.wenjoy.multiimagechooser.MainActivity;  
  12.   
  13. /** 
  14.  * js调用java方法 
  15.  *  
  16.  * 必须继承CordovaPlugin CordovaPlugin里面有实现cordovaActivity的方法 
  17.  * 提供startActivityForResult(); 
  18.  *  
  19.  * 我使用的 cordova 3.3.0版本 
  20.  *  
  21.  * @author XueQi 
  22.  *  
  23.  */  
  24. public class Plugin_intent extends CordovaPlugin {  
  25.     private String infos;  
  26.   
  27.     /** 
  28.      * 注意 构造方法不能为 
  29.      *  
  30.      * Plugin_intent(){} 
  31.      *  
  32.      * 可以不写或者 定义为如下 
  33.      *  
  34.      */  
  35.     public Plugin_intent() {  
  36.     }  
  37.   
  38.     CallbackContext callbackContext;  
  39.   
  40.     @Override  
  41.     public boolean execute(String action, org.json.JSONArray args,  
  42.             CallbackContext callbackContext) throws org.json.JSONException {  
  43.         this.callbackContext = callbackContext;  
  44.         Log.i("123", action);  
  45.   
  46.         if (action.equals("intent")) {  
  47.             // 获取JS传递的args的第一个参数  
  48.             infos = args.getString(0);  
  49.             this.function();  
  50.             return true;  
  51.         }  
  52.         return false;  
  53.   
  54.     }  
  55.   
  56.     // 方法执行体  
  57.     private void function() {  
  58.         // cordova.getActivity() 获取当前activity的this  
  59.         Log.i("123", cordova.getActivity().toString());  
  60.         Intent intent = new Intent(cordova.getActivity(), MainActivity.class);  
  61.         intent.putExtra("infos", infos);  
  62.         cordova.startActivityForResult((CordovaPlugin) this, intent, 200);  
  63.   
  64.     }  
  65.   
  66.     @Override  
  67.     public void onActivityResult(int requestCode, int resultCode, Intent intent) {  
  68.   
  69.         super.onActivityResult(requestCode, resultCode, intent);  
  70.         // 传递返回值 给js方法  
  71.         callbackContext.success(com.alibaba.fastjson.JSONArray  
  72.                 .toJSONString(ResponseJSON.getInstance().getJsonObjects()));  
  73.         if (ResponseJSON.getInstance().getJsonObjects() != null  
  74.                 && ResponseJSON.getInstance().getJsonObjects().size() > 0) {  
  75.             Toast.makeText(cordova.getActivity(), "恭喜,上传完成"1000).show();  
  76.         }  
  77.     }  
  78.   
  79. }  

    b.方法编写完成后,要在res/xml/config.xml下注册,写在widget标签中添加

  1. <feature name="Demo">  
  2.        <param name="android-package" value="plugins.Plugin_intent" /><!-- value:包名.类名 -->  
  3. </feature>  
    feature的name很重要,一会在JS中要用到。

    

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

  1. cordova.define("org.apache.cordova.intent", function(require, exports, module) { /*  
  2.  *  
  3.  * Licensed to the Apache Software Foundation (ASF) under one  
  4.  * or more contributor license agreements.  See the NOTICE file  
  5.  * distributed with this work for additional information  
  6.  * regarding copyright ownership.  The ASF licenses this file  
  7.  * to you under the Apache License, Version 2.0 (the  
  8.  * "License"); you may not use this file except in compliance  
  9.  * with the License.  You may obtain a copy of the License at  
  10.  *  
  11.  *   http://www.apache.org/licenses/LICENSE-2.0  
  12.  *  
  13.  * Unless required by applicable law or agreed to in writing,  
  14.  * software distributed under the License is distributed on an  
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  16.  * KIND, either express or implied.  See the License for the  
  17.  * specific language governing permissions and limitations  
  18.  * under the License.  
  19.  *  
  20. */  
  21.   
  22. var exec = require('cordova/exec');  
  23.   
  24. /**  
  25.  * Provides access to the vibration mechanism on the device.  
  26.  */  
  27.   
  28. module.exports = {  
  29.   
  30.     /**  
  31.      * 一共5个参数  
  32.        第一个 :成功会掉  
  33.        第二个 :失败回调  
  34.        第三个 :将要调用的类的配置名字(在config.xml中配置 稍后在下面会讲解)  
  35.        第四个 :调用的方法名(一个类里可能有多个方法 靠这个参数区分)  
  36.        第五个 :传递的参数  以json的格式  
  37.      */  
  38.     demo: function(mills) {  
  39.         exec(function(winParam){  
  40.             alert(winParam);<span style="font-family: Arial, Helvetica, sans-serif;">//执行成功,winParam是类中callbackContext.success传递的参数</span>  
  41.         }, null, "Demo", "intent", [mills]);  
  42.     },  
  43. };  
  44.   
  45. });  
    demo:定义被JS调用的方法名

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

    mills:这里我始终只能传递一个参数,所以,我现在的解决方式是拼接一个字符串,例如:'aaa,nnn,ccc',用逗号分割三个参数

Step3.使用插件

    截止到现在,整个插件就OK啦,可以创建一个html和Activiry了,这里我只列出LUNCH Activity的编写和html页面

    在assert/www下建立index.html文件,很简单

  1. <!DOCTYPE html>  
  2. <html>    
  3. <head>     
  4.   <title>Notification Example</title>   
  5.   <script type="text/javascript" charset="utf-8" src="cordova.js"></script>      
  6.   <script type="text/javascript" charset="utf-8">      
  7.    // Wait for device API libraries to load      
  8.    //      
  9.    document.addEventListener("deviceready", onDeviceReady, false);      
  10.    // device APIs are available      
  11.    //      
  12.     //       跳转  
  13.     function intent() {          navigator.intent.demo('NDljY2E1ZGM4NzUzM2U3Yg==,order,5740');          }    //token,eneityname,entityid  
  14.   
  15.     </script>    
  16.     </head>    
  17.     <body>      
  18.     <p><a href="#" onclick="intent(); return false;">Upload Image</a></p>    
  19.     </body>  
  20.     </html>  

    ViewActivity:
  1. package com.wenjoy.multiimagechooser;  
  2.   
  3. import org.apache.cordova.CordovaActivity;  
  4.   
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7.   
  8. /** 
  9.  * 装载HTML页面的Activity 
  10.  *  
  11.  * @author XueQi 
  12.  *  
  13.  */  
  14. public class ViewActivity extends CordovaActivity {  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         super.init();  
  19.         // Set by <content src="index.html" /> in config.xml  
  20.         super.loadUrl("file:///android_asset/www/index.html");  
  21.         // super.loadUrl("file:///android_asset/www/index.html")  
  22.     }  
  23.   
  24.     @Override  
  25.     protected void onActivityResult(int requestCode, int resultCode,  
  26.             Intent intent) {  
  27.         super.onActivityResult(requestCode, resultCode, intent);  
  28.   
  29.     }  
  30. }  


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值