android studio 开发 cordova plugin(组件)的 helloWorld

原创 2016年05月27日 16:46:50

第一步:

按照 http://blog.csdn.net/u010919133/article/details/51507343 上创建一个android project


第二步:

已完成的project结构预览



第三步:create MyPlugin.java文件


package oo.mobile;

import android.content.Intent;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by admin on 2016/5/27.
 */
public class MyPlugin extends CordovaPlugin {
    public static final String ACTION_ADD_CALENDAR_ENTRY = "addCalendarEntry";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        try {
            if (ACTION_ADD_CALENDAR_ENTRY.equals(action)) {
                JSONObject arg_object = args.getJSONObject(0);
                Intent calIntent = new Intent(Intent.ACTION_EDIT)
                        .setType("vnd.android.cursor.item/event")
                        .putExtra("beginTime", arg_object.getLong("startTimeMillis"))
                        .putExtra("endTime", arg_object.getLong("endTimeMillis"))
                        .putExtra("title", arg_object.getString("title"))
                        .putExtra("description", arg_object.getString("description"))
                        .putExtra("eventLocation", arg_object.getString("eventLocation"));

                this.cordova.getActivity().startActivity(calIntent);
                callbackContext.success();
                return true;
            }
            callbackContext.error("Invalid action");
            return false;
        } catch(Exception e) {
            System.err.println("Exception: " + e.getMessage());
            callbackContext.error(e.getMessage());
            return false;
        }
    }
}

第四步:在config.xml注册MyPlugin

在<widget>里面添加如下代码:

添加的代码(需要注意的是你的packge name!!!!!):

<feature name="MyPlugin">
    <param name="android-package" value="oo.mobile.MyPlugin" />
</feature>

添加后如图所示




第五步:在assets/www/js文件夹下创建carrier.js(名字你喜欢就行)

var calendarPlugin = { 
    createEvent: function(title, location, notes, startDate, endDate, successCallback, errorCallback) { 
        cordova.exec( 
            successCallback, // success callback function 
            errorCallback, // error callback function 
            'MyPlugin', // mapped to our native Java class called "CalendarPlugin"
            'addCalendarEntry', // with this action name 
            [{                  // and this array of custom arguments to create our entry 
                "title": title, 
                "description": notes, 
                "eventLocation": location, 
                "startTimeMillis": startDate.getTime(), 
                "endTimeMillis": endDate.getTime() 
            }] 
        );  
     } 
}

第六步:下面是index.html的实现(红色部分是添加的代码)

<!DOCTYPE html>
<!--
    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.
-->
<html>
<head>
    <!--
    Customize this policy to fit your own app's needs. For more guidance, see:
        https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
    Some notes:
        * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
        * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
        * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
            * Enable inline JS: add 'unsafe-inline' to default-src
    -->
    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport"
          content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <title>Hello World</title>
</head>
<body>
<button onclick="addToCal();">Carrier Code!</button>
<script type="text/javascript">
   function addToCal() {
            var startDate = new Date("July 19, 2013 8:00:00");
        var endDate = new Date("July 19, 2013 18:00:00");
        var notes = "Arrive on time, don't want to miss out (from Android)";
        var title = "PhoneGap Day";
        var location = "Portland, OR";
        var notes = "Arrive on time, don't want to miss out!";
        var success = function() { alert("Success"); };
        var error = function(message) { alert("Oopsie! " + message); };
        calendarPlugin.createEvent(title, location, notes, startDate, endDate, success, error);
    }
</script>
<script type="text/javascript" src="js/carrier.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>


效果图


相关文章推荐

Apache Cordova 在Android Studio中使用心得

1,Apache Cordova 环境搭建 参考:http://www.tuicool.com/articles/rimQjyU Cordova是Node.js的一款第三方依赖包,并托管到 htt...

关于在已有项目中手动添加cordova插件

在用H5,或者说cordova做开发的时候,不可避免的要增加新的插件,因为需求总变更啊,这一点长期和需求斗争的开发人员应该深有体会。关于在新项目中加插件,不再赘述,也很简单,困难的是在一个已有的项目中...

Cordova-Android 插件开发

Android 插件开发 原文地址:https://cordova.apache.org/docs/en/latest/guide/platforms/android/plugin.html An...

Android Cordova 插件开发之编写自定义插件

前言本文适合Android+web的复合型人才,因为cordova本身就是混合开发,所以在Android开发的基础上,还要懂web相关技术(HTML+CSS+JS),但是也有例外,比如我,只需负责An...

cordova插件开发,简单教程

cordova.exec(function(winParam) {}, function(error) {}, "service",...
  • yipanbo
  • yipanbo
  • 2015年08月28日 12:50
  • 6198

cordova插件:inappbrowser

原文网址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-plugin-inappbrowser/ 要想App...

Android基于cordova3.3的插件开发

最近工作的项目,需要用到cordova进行插件开发,具体Cordova的作用,就不再赘述,大家可以自行的去百度就OK了,直接开始。具体的流程,我将已一个小的Demo进行推进讲解。也是刚刚接触,太理论的...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

cordova 和android studio的第一个项目(常见问题注意!)

cordova 和android studio的第一个项目

Cordova自定义插件实战

Cordova自定义插件实战完成Toast,拨打电话,发送短信的插件编写
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android studio 开发 cordova plugin(组件)的 helloWorld
举报原因:
原因补充:

(最多只允许输入30个字)