zoukankan      html  css  js  c++  java
  • 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


            

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

  • 相关阅读:
    vue中Axios的封装和API接口的管理
    解决Vue报错:TypeError: Cannot read property 'scrollHeight' of undefined
    JS-scrollTop、scrollHeight、clientTop、clientHeight、offsetTop、offsetHeight的理解
    理解Vue中的ref和$refs
    理解Vue中的nextTick
    CSS——overflow的参数以及使用
    JS数据结构——队列
    Vue中实现聊天窗口overflow:auto自动滚动到底部,实现显示当前最新聊天消息
    Vue中无法检测到数组的变动
    JS-观察者模式
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4650680.html
Copyright © 2011-2022 走看看