• android日历调用首先第一步我们要添加权限
  <uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
  • 接着我们书写js调用接口
var calendar = {
add: function (success, data) {
exec(success, null, "Calendar", "add", [data]);
},
open:function(success, data){
exec(success, null, "Calendar", "open", [data]);
} }; module.exports = calendar;
  • 这两个方法一个add是后台添加到日历数据库,一个打开日历,然后调用系统日历添加

由于android存在版本兼容我们要添加

if (Build.VERSION.SDK_INT >=8) {
calanderURL = "content://com.android.calendar/calendars";
calanderEventURL = "content://com.android.calendar/events";
calanderRemiderURL = "content://com.android.calendar/reminders"; } else {
calanderURL = "content://calendar/calendars";
calanderEventURL = "content://calendar/events";
calanderRemiderURL = "content://calendar/reminders";
}
  • android的日历存储有多个用户,但是系统默认一般会第一个
Cursor userCursor = cordova.getActivity().getContentResolver()
.query(Uri.parse(calanderURL), null, null, null, null);
if (userCursor.getCount() > 0) {
userCursor.moveToFirst();
calId = userCursor.getString(userCursor.getColumnIndex("_id"));
}
  • android后台添加日历参数,这个时候我们一般调用phonegap线程写法,不会影响ui
private void Add(final JSONObject sData, final CallbackContext context) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
ContentValues event = new ContentValues();
Log.v(TAG, sData.getString("dtstart"));
event.put("calendar_id", calId);
event.put(Events.TITLE, sData.getString("title"));
event.put(Events.EVENT_LOCATION,
sData.getString("location"));
event.put(Events.DESCRIPTION,
sData.getString("description"));
event.put(Events.DTSTART, sData.getLong("dtstart"));
event.put(Events.DTEND, sData.getLong("dtend"));
event.put("eventTimezone", TimeZone.getDefault().getID());
Uri url = cordova.getActivity().getContentResolver()
.insert(Uri.parse(calanderEventURL), event);
long id = Long.parseLong(url.getLastPathSegment());
ContentValues values = new ContentValues();
values.put("event_id", id);
// 提前10分钟有提醒
values.put("minutes", 10);
values.put("method", 1);
cordova.getActivity().getContentResolver()
.insert(Uri.parse(calanderRemiderURL), values);
} catch (JSONException e) {
e.printStackTrace();
}
context.success(); // Thread-safe.
}
});
}
  • 如何调用android弹出系统添加页面了
private void Open() {
Uri uri = Uri.parse(calanderEventURL);
Intent intent = new Intent("android.intent.action.INSERT", uri);
lastId = GetLastId();
cordova.startActivityForResult((CordovaPlugin) this, intent, 1); }
  • 因为调用系统添加日历我们无法知道我们添加的状态,我们只能判断日历事件表最大id的是否变化
  • 接下来提供详细java代码
package com.triedtech.triedapp.plugins.calendar;

import java.util.TimeZone;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build; import android.provider.CalendarContract.Events;
import android.util.Log; public class Calendar extends CordovaPlugin {
public static final Integer RESULT_CODE_CREATE = 0;
private static String calanderURL = "";
private static String calanderEventURL = "";
private static String calanderRemiderURL = "";
private String TAG = "calendar";
public int isInit = 0;
public int lastId = 0;
private String calId;
private CallbackContext callbackContext; public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
initConfig();
if (action.equals("add")) { JSONObject sData = args.getJSONObject(0);
this.Add(sData, callbackContext);
return true;
} else if (action.equals("open")) {
Open();
return true;
}
return false;
} private void initConfig() {
if (isInit == 0) {
if (Build.VERSION.SDK_INT >=8) {
calanderURL = "content://com.android.calendar/calendars";
calanderEventURL = "content://com.android.calendar/events";
calanderRemiderURL = "content://com.android.calendar/reminders"; } else {
calanderURL = "content://calendar/calendars";
calanderEventURL = "content://calendar/events";
calanderRemiderURL = "content://calendar/reminders";
}
Cursor userCursor = cordova.getActivity().getContentResolver()
.query(Uri.parse(calanderURL), null, null, null, null);
if (userCursor.getCount() > 0) {
userCursor.moveToFirst();
calId = userCursor.getString(userCursor.getColumnIndex("_id"));
}
isInit = 1;
}
} private void Add(final JSONObject sData, final CallbackContext context) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
ContentValues event = new ContentValues();
Log.v(TAG, sData.getString("dtstart"));
event.put("calendar_id", calId);
event.put(Events.TITLE, sData.getString("title"));
event.put(Events.EVENT_LOCATION,
sData.getString("location"));
event.put(Events.DESCRIPTION,
sData.getString("description"));
event.put(Events.DTSTART, sData.getLong("dtstart"));
event.put(Events.DTEND, sData.getLong("dtend"));
event.put("eventTimezone", TimeZone.getDefault().getID());
Uri url = cordova.getActivity().getContentResolver()
.insert(Uri.parse(calanderEventURL), event);
long id = Long.parseLong(url.getLastPathSegment());
ContentValues values = new ContentValues();
values.put("event_id", id);
// 提前10分钟有提醒
values.put("minutes", 10);
values.put("method", 1);
cordova.getActivity().getContentResolver()
.insert(Uri.parse(calanderRemiderURL), values);
} catch (JSONException e) {
e.printStackTrace();
}
context.success(); // Thread-safe.
}
});
} private int GetLastId() {
Cursor userCursor = cordova
.getActivity()
.getContentResolver()
.query(Uri.parse(calanderEventURL), null,
Events.CALENDAR_ID + " =" + calId, null,
Events._ID + " desc limit 1");
if (userCursor.getCount() > 0) {
userCursor.moveToFirst();
String event_id = userCursor.getString(userCursor
.getColumnIndex(Events._ID));
userCursor.close();
return Integer.parseInt(event_id);
}
return 0;
} private void Open() {
Uri uri = Uri.parse(calanderEventURL);
Intent intent = new Intent("android.intent.action.INSERT", uri);
lastId = GetLastId();
cordova.startActivityForResult((CordovaPlugin) this, intent, 1); } private JSONObject LastRecord() throws JSONException{
Cursor userCursor = cordova
.getActivity()
.getContentResolver()
.query(Uri.parse(calanderEventURL), null,
Events.CALENDAR_ID + " =" + calId, null,
Events._ID + " desc limit 1");
userCursor.moveToFirst();
String event_id = userCursor.getString(userCursor
.getColumnIndex(Events._ID));
JSONObject record = new JSONObject();
Log.v(TAG, userCursor.getString(userCursor.getColumnIndex(Events.TITLE)));
record.put("dtstart", userCursor.getString(userCursor.getColumnIndex(Events.DTSTART)));
record.put("dtend", userCursor.getString(userCursor.getColumnIndex(Events.DTEND)));
record.put("id", userCursor.getString(userCursor.getColumnIndex(Events._ID)));
record.put("location", userCursor.getString(userCursor.getColumnIndex(Events.EVENT_LOCATION)));
record.put("title", userCursor.getString(userCursor.getColumnIndex(Events.TITLE)));
record.put("description", userCursor.getString(userCursor.getColumnIndex(Events.DESCRIPTION)));
userCursor.close(); return record;
} /**
* Called when the calendar view exits.
*
* @param requestCode
* The request code originally supplied to
* startActivityForResult(), allowing you to identify who this
* result came from.
* @param resultCode
* The integer result code returned by the child activity through
* its setResult().
* @param intent
* An Intent, which can return result data to the caller (various
* data can be attached to Intent "extras").
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.v(TAG,String.valueOf(lastId));
Log.v(TAG,String.valueOf(GetLastId()));
if (lastId != GetLastId()) {
try {
Log.v(TAG,"执行");
this.callbackContext.success(LastRecord());
} catch (JSONException e) {
e.printStackTrace();
}
}
} }

phonegap之android原生日历调用的更多相关文章

  1. uni-app&H5&Android混合开发三 || uni-app调用Android原生方法的三种方式

    前言: 关于H5的调用Android原生方法的方式有很多,在该片文章中我主要简单介绍三种与Android原生方法交互的方式. 一.H5+方法调用android原生方法 H5+ Android开发规范官 ...

  2. PhoneGap或者Cordova框架下实现Html5中JS调用Android原生代码

    PhoneGap或者Cordova框架下实现Html5中JS调用Android原生代码 看看新闻网>看引擎>开源产品 0人收藏此文章, 发表于8小时前(2013-09-06 00:39) ...

  3. Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例

    引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...

  4. Flutter学习(9)——Flutter插件实现(Flutter调用Android原生

    原文地址: Flutter学习(9)--Flutter插件实现(Flutter调用Android原生) | Stars-One的杂货小窝 最近需要给一个Flutter项目加个apk完整性检测,需要去拿 ...

  5. React Native Android原生模块开发实战|教程|心得|怎样创建React Native Android原生模块

    尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 告诉大家一个好消息. ...

  6. Android原生游戏开发:使用JustWeEngine开发微信打飞机

    使用JustWeEngine开发微信打飞机: 作者博客: 博客园 引擎地址:JustWeEngine 示例代码:EngineDemo JustWeEngine? JustWeEngine是托管在Git ...

  7. 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏

    也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...

  8. 使用 Eclipse PhoneGap 构建 Android 应用程序入门

    Eclipse 是一种支持多种技术的开源集成开发环境 (IDE),但本文重点介绍 Java 支持,这也是 Android 应用程序的“母语”.Android 是 Google 发布的开源移动操作系统. ...

  9. [Android Pro] android 4.4 Android原生权限管理:AppOps

    reference : http://m.blog.csdn.net/blog/langzxz/45308199 reference : http://blog.csdn.net/hyhyl1990/ ...

随机推荐

  1. Python操作Mysql之基本操作

    pymysql python操作mysql依赖pymysql这个模块 下载安装 pip3 install pymysql 操作mysql python操作mysql的时候,是通过”游标”来进行操作的. ...

  2. C#安全性记录

    安全性一直是开发中,重中之重的问题.不过平时用的不算特别多,基本上用个MD5,SSL也就到这了.再次记录一下,以免忘记. MD5多次加密 MD5算法是不可逆算法.应用于密码验证,完整性验证这种特征.这 ...

  3. MySQL索引背后的数据结构及算法原理

    摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...

  4. linux REDHAT6.4下安装ArcGIS Server 10.1

    1 安装环境 因为Linux的发行版本比较多,我们在使用的时候请严格按照官网给的给出的版本,在官网上给出的是经过严格测试的,如果采用其他的,即便安装上了,在后续的运作中出现问题,这个可就麻烦了,官网对 ...

  5. java 追加文件

    FileWriter writer = null; String fileName="d://my.txt"; String line="要追加的内容"; tr ...

  6. Schema

    Schema约束 1.namespace 相当于schema文件的id 2.targetNamespace属性 用来指定schema文件的namespace的值 3.xmlns属性 引入一个约束,它的 ...

  7. 2014 Multi-University Training Contest 9#11

    2014 Multi-University Training Contest 9#11 Killing MonstersTime Limit: 2000/1000 MS (Java/Others)   ...

  8. thinkphp3.2跨控制器调用其他模块的方法

    thinphp中前台后台都有互相调用方法,这样可以省去重复内容. 1 2 $hello = new \Admin\Common\Fun\hello(); $hello->hehe(); 调用其他 ...

  9. PYTHON 写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容。

    def shifou_space(args): ret = True for a in args: if a.isspace(): ret = False break return ret resul ...

  10. Qt应用程序图标设置

    Qt应用程序图标设置 本文仅仅适用于windows下,linux等不适用. 下面说的图标,指的是程序文件的图标,而不是托盘图标或者说运行时任务栏的图标(任务栏和程序窗口的图标在windows/linu ...