phonegap之android原生日历调用
- 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原生日历调用的更多相关文章
- uni-app&H5&Android混合开发三 || uni-app调用Android原生方法的三种方式
前言: 关于H5的调用Android原生方法的方式有很多,在该片文章中我主要简单介绍三种与Android原生方法交互的方式. 一.H5+方法调用android原生方法 H5+ Android开发规范官 ...
- PhoneGap或者Cordova框架下实现Html5中JS调用Android原生代码
PhoneGap或者Cordova框架下实现Html5中JS调用Android原生代码 看看新闻网>看引擎>开源产品 0人收藏此文章, 发表于8小时前(2013-09-06 00:39) ...
- Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例
引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...
- Flutter学习(9)——Flutter插件实现(Flutter调用Android原生
原文地址: Flutter学习(9)--Flutter插件实现(Flutter调用Android原生) | Stars-One的杂货小窝 最近需要给一个Flutter项目加个apk完整性检测,需要去拿 ...
- React Native Android原生模块开发实战|教程|心得|怎样创建React Native Android原生模块
尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 告诉大家一个好消息. ...
- Android原生游戏开发:使用JustWeEngine开发微信打飞机
使用JustWeEngine开发微信打飞机: 作者博客: 博客园 引擎地址:JustWeEngine 示例代码:EngineDemo JustWeEngine? JustWeEngine是托管在Git ...
- 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏
也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...
- 使用 Eclipse PhoneGap 构建 Android 应用程序入门
Eclipse 是一种支持多种技术的开源集成开发环境 (IDE),但本文重点介绍 Java 支持,这也是 Android 应用程序的“母语”.Android 是 Google 发布的开源移动操作系统. ...
- [Android Pro] android 4.4 Android原生权限管理:AppOps
reference : http://m.blog.csdn.net/blog/langzxz/45308199 reference : http://blog.csdn.net/hyhyl1990/ ...
随机推荐
- Django学习笔记(现学现写,实时更新)
说明:我是先上手做一些简单的例子,然后在尝试的过程中理解Django的原理,笔记也是按这个思路来的. 一.Django结构与基本文件介绍 1. django-admin.py 工程管理工具,主要用于创 ...
- sublime text 3 3114 注册码
-– BEGIN LICENSE -– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 AFF6F6 ...
- bzoj 2301: [HAOI2011]Problem b
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...
- Promise deeply lookup
Motivation Consider the following synchronous JavaScript function to read a file and parse it as JSO ...
- python中文分词:结巴分词
中文分词是中文文本处理的一个基础性工作,结巴分词利用进行中文分词.其基本实现原理有三点: 基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG) 采用了动态规 ...
- c#.Net:Excel导入/导出之NPOI 2.0简介
NPOI 2.0+主要由SS, HPSF, DDF, HSSF, XWPF, XSSF, OpenXml4Net, OpenXmlFormats组成,具体列表如下: 资料来自:百度百科 Ass ...
- 【JWT】JWT+HA256加密 Token验证
目录 Token验证 传统的Token验证 JWT+HA256验证 回到顶部 Token验证 最近了解下基于 Token 的身份验证,跟大伙分享下.很多大型网站也都在用,比如 Facebook,Twi ...
- Coursera-Getting and Cleaning Data-Week3-dplyr+tidyr+lubridate的组合拳
Coursera-Getting and Cleaning Data-Week3 Wednesday, February 04, 2015 好久不写笔记了,年底略忙.. Getting and Cle ...
- 使用OLEQAxObject导出Log日志文件
头文件<QAxObject> Qt project settings需要支持QtActivex container 具体导出如下:(单列这里) QString filepath=QFile ...
- spring常见问题
问题1:提示说:cvc-elt.1: Cannot find the declaration of element 'beans' 解决方法:从网上搜了一些,有的说是因为网络原因访问不到xsd文件,因 ...