Android日历提醒是非常好的提醒功能,笔者在做的过程中,遇到的一些问题,现整理出来,以供参考。
一、申请日历的读写权限
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
如果需要读取日历数据,需要加入READ_CALENDAR权限。若需要对日历数据进行删改操作,需要WRITE_CALENDAR权限。
二、事件表
CalendarContract.Events表包含事件的信息,比如标题、描述、开始时间等,如需查看所支持字段的完整列表,请参阅 CalendarContract.Events。
常量 | 描述 |
---|---|
_ID | 事件ID,非常重要,更新删除数据都要用到。 |
CALENDAR_ID | 事件所属日历用户ID。 |
TITLE | 事件的名称。 |
DESCRIPTION | 事件的描述。 |
DTSTART | 事件开始时间,以从公元纪年开始计算的协调世界时毫秒数表示。 |
DTEND | 事件结束时间,以从公元纪年开始计算的协调世界时毫秒数表示。 |
EVENT_TIMEZONE | 事件的时区。 |
EVENT_END_TIMEZONE | 事件结束时间的时区。 |
DURATION | RFC5545 格式的事件持续时间。例如,值为 "PT1H" 表示事件应持续一小时,值为 "P2W" 表示持续 2 周。 |
ALL_DAY | 值为 1 表示此事件占用一整天(按照本地时区的定义)。值为 0 表示它是常规事件,可在一天内的任何时间开始和结束。 |
RRULE | 事件的重复发生规则格式。例如,"FREQ=WEEKLY;COUNT=10;WKST=SU" 。您可以在此处找到更多示例。 |
RDATE | 事件的重复发生日期。RDATE 与 RRULE 通常联合用于定义一组存在聚合关系的重复实例。如需查看更详细的介绍,请参阅 RFC5545 规范。 |
AVAILABILITY | 将此事件视为忙碌时间还是可调度的空闲时间。 |
HAS_ALARM | 设置是否有闹钟提醒,0-没有,1-有 |
三、日历用户
增加事件需要用户信息(CalendarContract.Events.CALENDAR_ID),相关代码如下:
private static final String CALENDER_URL = "content://com.android.calendar/calendars";private static final String CALENDARS_NAME = "XX";// 随便写
private static final String CALENDARS_ACCOUNT_NAME = "XX";// 随便写
private static final String CALENDARS_ACCOUNT_TYPE = CalendarContract.ACCOUNT_TYPE_LOCAL;
private static final String CALENDARS_DISPLAY_NAME = StringUtils.getString(R.string.app_name) + "账户";// 随便写/*** 检查是否已经添加了日历账户,如果没有添加先添加一个日历账户再查询* @return 获取账户成功返回账户id,否则返回-1*/
private static int checkAndAddCalendarAccount(Context context) {int oldId = checkCalendarAccount(context);if (oldId >= 0) {return oldId;} else {long addId = addCalendarAccount(context);if (addId >= 0) {return checkCalendarAccount(context);} else {return -1;}}
}/*** @return 检查是否存在现有账户,存在则返回账户id,否则返回-1*/
private static int checkCalendarAccount(Context context) {Cursor userCursor = context.getContentResolver().query(Uri.parse(CALENDER_URL), null, null, null, null);try {if (userCursor == null) { //查询返回空值return -1;}int columnIndex = userCursor.getColumnIndex(CalendarContract.Calendars._ID);int count = userCursor.getCount();if (count > 0) { //存在现有账户,取第一个账户的id返回userCursor.moveToFirst();return userCursor.getInt(columnIndex);} else {return -1;}} catch (Exception ex) {// do nothingreturn -1;} finally {if (userCursor != null) {userCursor.close();}}
}/*** @return 添加日历账户,账户创建成功则返回账户id,否则返回-1*/
private static long addCalendarAccount(Context context) {TimeZone timeZone = TimeZone.getDefault();ContentValues value = new ContentValues();value.put(CalendarContract.Calendars.NAME, CALENDARS_NAME);value.put(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_ACCOUNT_NAME);value.put(CalendarContract.Calendars.ACCOUNT_TYPE, CALENDARS_ACCOUNT_TYPE);value.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CALENDARS_DISPLAY_NAME);value.put(CalendarContract.Calendars.VISIBLE, 1);value.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.BLUE);value.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);value.put(CalendarContract.Calendars.SYNC_EVENTS, 1);value.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, timeZone.getID());value.put(CalendarContract.Calendars.OWNER_ACCOUNT, CALENDARS_ACCOUNT_NAME);value.put(CalendarContract.Calendars.CAN_ORGANIZER_RESPOND, 0);Uri calendarUri = Uri.parse(CALENDER_URL);calendarUri = calendarUri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_ACCOUNT_NAME).appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CALENDARS_ACCOUNT_TYPE).build();Uri result = context.getContentResolver().insert(calendarUri, value);return result == null ? -1 : ContentUris.parseId(result);
}
四、增加日历事件
先创建日历事件,再将事件加入日历提醒,代码如下:
private static final String CALENDER_EVENT_URL = "content://com.android.calendar/events";
private static final String CALENDER_REMINDER_URL = "content://com.android.calendar/reminders";
private static final String[] PERMISSION_LIST = {Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR};/*** 真正添加日历事件** @param context:上下文* @param map:日历事件的信息* @return 返回事件id*/
private static long addCalendarEvent(Context context, @NonNull Map<String, Object> map) {if (context == null) {return 0;}if (!XXPermissions.isGranted(context, PERMISSION_LIST)) {return 0;}int calId = checkAndAddCalendarAccount(context); //获取日历账户的idif (calId < 0) { //获取账户id失败直接返回,添加日历事件失败return 0;}String title = (String) map.get("title");String description = (String) map.get("description");long start = 0;Object originStartTime = map.get("start_time");if (originStartTime instanceof Long) {start = (Long) originStartTime;}String rule = (String) map.get("rule");ContentValues event = new ContentValues();event.put(CalendarContract.Events.TITLE, title);event.put(CalendarContract.Events.DESCRIPTION, description);event.put(CalendarContract.Events.CALENDAR_ID, calId); //插入账户的idevent.put(CalendarContract.Events.DTSTART, start);//event.put(CalendarContract.Events.DTEND, end);// 与DURATION不能同时设置event.put(CalendarContract.Events.HAS_ALARM, 1);//设置有闹钟提醒event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());//这个是时区,必须有event.put(CalendarContract.Events.DURATION, "P0S");// 重复设置必须设置,否则日期有问题if (!StringUtils.isEmpty(rule)) {event.put(CalendarContract.Events.RRULE, rule);// 重复规则 rrule(Recurrence Rule)}Uri newEvent = context.getContentResolver().insert(Uri.parse(CALENDER_EVENT_URL), event); //添加事件if (newEvent == null) { //添加日历事件失败直接返回LogUtils.d("addCalendarEvent2 newEvent 添加日历事件失败直接返回");return 0;}//事件提醒的设定ContentValues values = new ContentValues();values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));values.put(CalendarContract.Reminders.MINUTES, 0);// 提前0分钟提醒values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);Uri reminderEvent = context.getContentResolver().insert(Uri.parse(CALENDER_REMINDER_URL), values);if (reminderEvent == null) {LogUtils.d("addCalendarEvent2 reminderEvent 添加日历事件失败直接返回");}return Long.parseLong(newEvent.getLastPathSegment());
}
总结下增加事件的规则:
- 您必须加入 CALENDAR_ID 和 DTSTART。
- 您必须加入 EVENT_TIMEZONE。如需获取系统中已安装时区 ID 的列表,请使用 getAvailableIDs()。请注意,如果您按使用 Intent 插入事件中所述通过 INSERT Intent 插入事件,则此规则不适用 — 在该情形下,系统会提供默认时区。
- 对于非重复事件,您必须加入 DTEND。
- 对于重复事件,您必须加入 DURATION,以及 RRULE 或 RDATE。请注意,如果您按使用 Intent 插入事件中所述通过 INSERT Intent 插入事件,则此规则不适用 — 在该情形下,您可以将 RRULE 与 DTSTART 和 DTEND 结合使用,日历应用会自动将其转换为持续时间。
- RRULE规则可参考:iCalendar Recurrence Rule 规范翻译 - 简书 (jianshu.com)
碎碎念:
1、之前添加重复事件,只设置 DTEND ,导致添加的天数有问题,改成DURATION就好了。
2、_ID(事件ID)非常重要,增加日历提醒后需要保存起来,后续的删改查都需要这个信息。
五、查找日历事件
查找比较简单了,这里使用事件ID(_ID),代码如下:
/*** 检查日历事件** @param context:* @param eventId:* @return 返回事件是否存在。true-事件存在,false-事件不存在*/
public static boolean checkCalendarEvent(Context context, long eventId) {if (context == null || eventId <= 0) {return false;}if (!XXPermissions.isGranted(context, PERMISSION_LIST)) {return false;}Uri uri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), eventId);Cursor eventCursor = context.getContentResolver().query(uri, null, null, null, null);if (eventCursor == null) {return false;}boolean result = eventCursor.getCount() > 0;eventCursor.close();return result;
}
六、修改日历事件
public static void updateCalendarEvent(Context context, long eventId) {if (context == null || eventId <= 0) {return;}if (!XXPermissions.isGranted(context, PERMISSION_LIST)) {return;}if (!checkCalendarEvent(context, eventId)) {return;}try {Uri uri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), eventId);ContentValues values = new ContentValues();values.put(CalendarContract.Events.TITLE, "XXXX");int rows = context.getContentResolver().update(uri, values, null, null);LogUtils.d("updateCalendarEvent rows=" + rows);} catch (Exception e) {e.printStackTrace();}
}
七、删除日历事件
/*** 删除日历事件*/
public static void deleteCalendarEvent(Context context, long eventId) {if (context == null || eventId <= 0) {return;}if (!XXPermissions.isGranted(context, PERMISSION_LIST)) {return;}if (!checkCalendarEvent(context, eventId)) {return;}try {Uri uri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), eventId);int rows = context.getContentResolver().delete(uri, null, null);LogUtils.d("deleteCalendarEventById rows=" + rows);} catch (Exception e) {e.printStackTrace();}
}
八、完整代码
import android.Manifest;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.provider.CalendarContract;
import android.text.TextUtils;
import android.util.ArrayMap;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.hjq.permissions.XXPermissions;import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;/*** 日历提醒工具类*/
public class CalendarReminderUtils {private static final String CALENDER_URL = "content://com.android.calendar/calendars";private static final String CALENDER_EVENT_URL = "content://com.android.calendar/events";private static final String CALENDER_REMINDER_URL = "content://com.android.calendar/reminders";private static final String CALENDARS_NAME = "XXX";private static final String CALENDARS_ACCOUNT_NAME = "XXX";private static final String CALENDARS_ACCOUNT_TYPE = CalendarContract.ACCOUNT_TYPE_LOCAL;private static final String CALENDARS_DISPLAY_NAME = StringUtils.getString(R.string.app_name) + "账户";private static final String[] PERMISSION_LIST = {Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR};/*** 检查是否已经添加了日历账户,如果没有添加先添加一个日历账户再查询* @return 获取账户成功返回账户id,否则返回-1*/private static int checkAndAddCalendarAccount(Context context) {int oldId = checkCalendarAccount(context);if (oldId >= 0) {return oldId;} else {long addId = addCalendarAccount(context);if (addId >= 0) {return checkCalendarAccount(context);} else {return -1;}}}/*** @return 检查是否存在现有账户,存在则返回账户id,否则返回-1*/private static int checkCalendarAccount(Context context) {Cursor userCursor = context.getContentResolver().query(Uri.parse(CALENDER_URL), null, null, null, null);try {if (userCursor == null) { //查询返回空值return -1;}int columnIndex = userCursor.getColumnIndex(CalendarContract.Calendars._ID);int count = userCursor.getCount();if (count > 0) { //存在现有账户,取第一个账户的id返回userCursor.moveToFirst();return userCursor.getInt(columnIndex);} else {return -1;}} catch (Exception ex) {// do nothingreturn -1;} finally {if (userCursor != null) {userCursor.close();}}}/*** @return 添加日历账户,账户创建成功则返回账户id,否则返回-1*/private static long addCalendarAccount(Context context) {TimeZone timeZone = TimeZone.getDefault();ContentValues value = new ContentValues();value.put(CalendarContract.Calendars.NAME, CALENDARS_NAME);value.put(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_ACCOUNT_NAME);value.put(CalendarContract.Calendars.ACCOUNT_TYPE, CALENDARS_ACCOUNT_TYPE);value.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CALENDARS_DISPLAY_NAME);value.put(CalendarContract.Calendars.VISIBLE, 1);value.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.BLUE);value.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);value.put(CalendarContract.Calendars.SYNC_EVENTS, 1);value.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, timeZone.getID());value.put(CalendarContract.Calendars.OWNER_ACCOUNT, CALENDARS_ACCOUNT_NAME);value.put(CalendarContract.Calendars.CAN_ORGANIZER_RESPOND, 0);Uri calendarUri = Uri.parse(CALENDER_URL);calendarUri = calendarUri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_ACCOUNT_NAME).appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CALENDARS_ACCOUNT_TYPE).build();Uri result = context.getContentResolver().insert(calendarUri, value);return result == null ? -1 : ContentUris.parseId(result);}/*** 添加日历事件--就寝提醒** @param context 上下文* @param title 提醒标题* @param description 提醒描述* @param reminderTime 就寝时间,格式HH:mm*/public static long addCalendarFromSleepAlarm(Context context, String title, String description, @Nullable String reminderTime) {Map<String, Object> map = new ArrayMap<>();map.put("title", title);map.put("description", description);map.put("rule", "FREQ=DAILY;COUNT=365");if (reminderTime == null) {reminderTime = "22:00";}String[] split = reminderTime.split(":");if (split.length != 2) {return 0;}Calendar calendar = Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(split[0]));calendar.set(Calendar.MINUTE, Integer.parseInt(split[1]));calendar.set(Calendar.SECOND, 0);long start = calendar.getTime().getTime();map.put("start_time", start);try {return addCalendarEvent(context, map);} catch (Exception e) {ExceptionManager.get().report(e);}return 0;}/*** 真正添加日历事件** @param context:上下文* @param map:日历事件的信息* @return 返回事件id*/private static long addCalendarEvent(Context context, @NonNull Map<String, Object> map) {if (context == null) {return 0;}if (!XXPermissions.isGranted(context, PERMISSION_LIST)) {return 0;}int calId = checkAndAddCalendarAccount(context); //获取日历账户的idLogUtils.d("addCalendarEvent checkAndAddCalendarAccount calId=" + calId);if (calId < 0) { //获取账户id失败直接返回,添加日历事件失败return 0;}String title = (String) map.get("title");String description = (String) map.get("description");long start = 0;Object originStartTime = map.get("start_time");if (originStartTime instanceof Long) {start = (Long) originStartTime;}String rule = (String) map.get("rule");ContentValues event = new ContentValues();event.put(CalendarContract.Events.TITLE, title);event.put(CalendarContract.Events.DESCRIPTION, description);event.put(CalendarContract.Events.CALENDAR_ID, calId); //插入账户的idevent.put(CalendarContract.Events.DTSTART, start);//event.put(CalendarContract.Events.DTEND, end);// 与DURATION不能同时设置event.put(CalendarContract.Events.HAS_ALARM, 1);//设置有闹钟提醒event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());//这个是时区,必须有event.put(CalendarContract.Events.DURATION, "P0S");// 重复设置必须设置,否则日期有问题if (!StringUtils.isEmpty(rule)) {event.put(CalendarContract.Events.RRULE, rule);// 重复规则 rrule(Recurrence Rule)}Uri newEvent = context.getContentResolver().insert(Uri.parse(CALENDER_EVENT_URL), event); //添加事件if (newEvent == null) { //添加日历事件失败直接返回LogUtils.d("addCalendarEvent2 newEvent 添加日历事件失败直接返回");return 0;}//事件提醒的设定ContentValues values = new ContentValues();values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));values.put(CalendarContract.Reminders.MINUTES, 0);// 提前0分钟提醒values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);Uri reminderEvent = context.getContentResolver().insert(Uri.parse(CALENDER_REMINDER_URL), values);if (reminderEvent == null) {LogUtils.d("addCalendarEvent2 reminderEvent 添加日历事件失败直接返回");}LogUtils.d("addCalendarEvent 事件id=" + Long.parseLong(newEvent.getLastPathSegment()));return Long.parseLong(newEvent.getLastPathSegment());}/*** 检查日历事件** @param context:* @param eventId:* @return 返回事件是否存在。true-事件存在,false-事件不存在*/public static boolean checkCalendarEvent(Context context, long eventId) {if (context == null || eventId <= 0) {return false;}if (!XXPermissions.isGranted(context, PERMISSION_LIST)) {return false;}Uri uri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), eventId);Cursor eventCursor = context.getContentResolver().query(uri, null, null, null, null);if (eventCursor == null) {return false;}boolean result = eventCursor.getCount() > 0;LogUtils.d("checkCalendarEvent eventCursor=" + eventCursor.getCount() + ",result=" + result);eventCursor.close();return result;}/*** 删除日历事件*/public static void deleteCalendarEvent(Context context, long eventId) {if (context == null || eventId <= 0) {return;}if (!XXPermissions.isGranted(context, PERMISSION_LIST)) {return;}if (!checkCalendarEvent(context, eventId)) {return;}try {Uri uri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), eventId);int rows = context.getContentResolver().delete(uri, null, null);LogUtils.d("deleteCalendarEventById rows=" + rows);} catch (Exception e) {e.printStackTrace();}}public static void updateCalendarEvent(Context context, long eventId) {if (context == null || eventId <= 0) {return;}if (!XXPermissions.isGranted(context, PERMISSION_LIST)) {return;}if (!checkCalendarEvent(context, eventId)) {return;}try {Uri uri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), eventId);ContentValues values = new ContentValues();values.put(CalendarContract.Events.TITLE, "XXXX");int rows = context.getContentResolver().update(uri, values, null, null);LogUtils.d("updateCalendarEvent rows=" + rows);} catch (Exception e) {e.printStackTrace();}}
}
参考文章:
Android 日历表事件表操作_android 读写系统日历 删除-CSDN博客
iCalendar Recurrence Rule 规范翻译 - 简书 (jianshu.com)