经常购买火车票,机票的同学就知道,当我们买下一张票的时候,票的行程日期会被写入系统日历中,当火车开动或者飞机启航的前30分钟,手机会有提醒信息,这条信息是由系统日历发出的,提醒用户,别错过时间啦。
像这种系统日历提醒功能,实现起来并不难,毕竟Android 系统已经提供API给我们调用了,不需要重新造轮子,下面我们来实现这个功能。
一、先获取读写日历的权限
系统怎么会提醒,是因为我们告诉了它时间,那么我们如何告诉日历,那就是像日历写入火车票的开动日期,一般都是读写权限都申请
在AndroidManifest.xml申请
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
6.0以上系统需要动态申请
if ((ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED)&&(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED)) {
flag = false;
}else{
flag = true;
}
二、获取日历账户
这里分两步,先检查是否存在日历账户,没有则添加新的日历账户,有则直接获取原有日历账户。
检查日历账号代码如下:
/**
* 检查是否存在现有账户,存在则返回账户id,否则返回-1
*/
private int checkCalendarAccount(Context context) {
Cursor userCursor = context.getContentResolver().query(Uri.parse(CALENDER_URL), null, null, null, null);
try {
if (userCursor == null) { //查询返回空值
return -1;
}
int count = userCursor.getCount();
if (count > 0) { //存在现有账户,取第一个账户的id返回
userCursor.moveToFirst();
return userCursor.getInt(userCursor.getColumnIndex(CalendarContract.Calendars._ID));
} else {
return -1;
}
} finally {
if (userCursor != null) {
userCursor.close();
}
}
}
根据检查返回结果是否是-1来判读,如果是-1,则添加日历账户
/**
* 添加日历账户,账户创建成功则返回账户id,否则返回-1
*/
private 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);
long id = result == null ? -1 : ContentUris.parseId(result);
return id;
}
三、添加日历事件,也就是先系统日历写入日期
这个过程可以添加日历事件的名称、描述、开始时间,结束时间、以及在开始时间前多少分钟提醒(通过测试,这个提前几分钟提醒至少是5分钟),设置是否开启闹钟提醒,提醒方法等。
/**
* 添加日历事件
*
* @param context
* @param title 标题
* @param description 内容
* @param reminderTime 提醒时间的时间戳
* @param endTime 事件结束时间戳
* @param previousDate 提前多少分钟提醒
*/
public boolean addCalendarEvent(Context context, String title, String description, long reminderTime, long endTime, int previousDate) {
if (context == null) {
return false;
}
int calId = checkAndAddCalendarAccount(context); //获取日历账户的id
if (calId < 0) { //获取账户id失败直接返回,添加日历事件失败
return false;
}
//添加日历事件
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, title);
event.put(CalendarContract.Events.DESCRIPTION, description);
event.put(CalendarContract.Events.CALENDAR_ID, calId); //插入账户的id
event.put(CalendarContract.Events.DTSTART, reminderTime);
event.put(CalendarContract.Events.DTEND, endTime);
event.put(CalendarContract.Events.HAS_ALARM, 1);//设置有闹钟提醒
event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getDisplayName());//这个是时区,必须有
Uri newEvent = context.getContentResolver().insert(Uri.parse(CALENDER_EVENT_URL), event); //添加事件
if (newEvent == null) { //添加日历事件失败直接返回
return false;
}
//事件提醒的设定
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));
values.put(CalendarContract.Reminders.MINUTES, previousDate);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = context.getContentResolver().insert(Uri.parse(CALENDER_REMINDER_URL), values);
if (uri == null) { //添加事件提醒失败直接返回
return false;
}
return true;
}
四、删除日历事件,既然可以添加,那就可以删除,删除的方法有两种,一种是直接打开手机日历,找到添加的日历事件,直接删除即可,另外一种是通过代码去删除添加过的日历事件,传入一个title参数,匹配到相应的标题,就删除掉它,这里会存在删除相同标题的情况,因为添加日历事件的时候,是可以添加两个相同标题的事件的。
/**
* 删除日历事件
*/
public void deleteCalendarEvent(Context context, String title) {
if (context == null) {
return;
}
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null, null, null, null);
try {
if (eventCursor == null) { //查询返回空值
return;
}
if (eventCursor.getCount() > 0) {
//遍历所有事件,找到title跟需要查询的title一样的项
for (eventCursor.moveToFirst(); !eventCursor.isAfterLast(); eventCursor.moveToNext()) {
String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
if (!TextUtils.isEmpty(title) && title.equals(eventTitle)) {
int id = eventCursor.getInt(eventCursor.getColumnIndex(CalendarContract.Calendars._ID));//取得id
Uri deleteUri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), id);
int rows = context.getContentResolver().delete(deleteUri, null, null);
if (rows == -1) { //事件删除失败
return;
}
}
}
}
} finally {
if (eventCursor != null) {
eventCursor.close();
}
}
}
}
以上就是所有的内容,我把代码封装成一个工具类,使用的时候直接调用添加日历事件方法跟删除日历事件方法即可。举个添加的栗子
Calendar beginTime = Calendar.getInstance();
beginTime.set(2022, 4, 20, 12, 0);
long startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2022, 4, 20, 15, 0);
long endMillis = endTime.getTimeInMillis();
CalendarEventsUtils.getInstance().addCalendarEvent(this,"火车票","别拖拖拉拉的,还有5分钟火车就开动了,抓紧~",startMillis,endMillis,5);
以下是完整代码:
public class CalendarEventsUtils {
private final String CALENDER_URL = "content://com.android.calendar/calendars";
private final String CALENDER_EVENT_URL = "content://com.android.calendar/events";
private final String CALENDER_REMINDER_URL = "content://com.android.calendar/reminders";
private final String CALENDARS_NAME = "日历";
private final String CALENDARS_ACCOUNT_NAME = "calendar";
private final String CALENDARS_ACCOUNT_TYPE = "com.example.calendar";
private final String CALENDARS_DISPLAY_NAME = "日历";
/**
* instance 单例
* INSTANCE_LOCK 互斥锁
*/
private static CalendarEventsUtils instance;
private static final Object INSTANCE_LOCK = new Object();
/**
* 获取单例
*
*/
public static CalendarEventsUtils getInstance() {
if (instance == null) {
synchronized (INSTANCE_LOCK) {
if (instance == null) {
instance = new CalendarEventsUtils();
}
}
}
return instance;
}
/**
* 检查是否已经添加了日历账户,如果没有添加先添加一个日历账户再查询
* 获取账户成功返回账户id,否则返回-1
*/
private 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;
}
}
}
/**
* 检查是否存在现有账户,存在则返回账户id,否则返回-1
*/
private int checkCalendarAccount(Context context) {
Cursor userCursor = context.getContentResolver().query(Uri.parse(CALENDER_URL), null, null, null, null);
try {
if (userCursor == null) { //查询返回空值
return -1;
}
int count = userCursor.getCount();
if (count > 0) { //存在现有账户,取第一个账户的id返回
userCursor.moveToFirst();
return userCursor.getInt(userCursor.getColumnIndex(CalendarContract.Calendars._ID));
} else {
return -1;
}
} finally {
if (userCursor != null) {
userCursor.close();
}
}
}
/**
* 添加日历账户,账户创建成功则返回账户id,否则返回-1
*/
private 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);
long id = result == null ? -1 : ContentUris.parseId(result);
return id;
}
/**
* 添加日历事件
*
* @param context
* @param title 标题
* @param description 内容
* @param reminderTime 提醒时间的时间戳
* @param endTime 事件结束时间戳
* @param previousDate 提前多少分钟提醒
*/
public boolean addCalendarEvent(Context context, String title, String description, long reminderTime, long endTime, int previousDate) {
if (context == null) {
return false;
}
int calId = checkAndAddCalendarAccount(context); //获取日历账户的id
if (calId < 0) { //获取账户id失败直接返回,添加日历事件失败
return false;
}
//添加日历事件
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, title);
event.put(CalendarContract.Events.DESCRIPTION, description);
event.put(CalendarContract.Events.CALENDAR_ID, calId); //插入账户的id
event.put(CalendarContract.Events.DTSTART, reminderTime);
event.put(CalendarContract.Events.DTEND, endTime);
event.put(CalendarContract.Events.HAS_ALARM, 1);//设置有闹钟提醒
event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getDisplayName());//这个是时区,必须有
Uri newEvent = context.getContentResolver().insert(Uri.parse(CALENDER_EVENT_URL), event); //添加事件
if (newEvent == null) { //添加日历事件失败直接返回
return false;
}
//事件提醒的设定
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));
values.put(CalendarContract.Reminders.MINUTES, previousDate);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = context.getContentResolver().insert(Uri.parse(CALENDER_REMINDER_URL), values);
if (uri == null) { //添加事件提醒失败直接返回
return false;
}
return true;
}
/**
* 删除日历事件
*/
public void deleteCalendarEvent(Context context, String title) {
if (context == null) {
return;
}
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null, null, null, null);
try {
if (eventCursor == null) { //查询返回空值
return;
}
if (eventCursor.getCount() > 0) {
//遍历所有事件,找到title跟需要查询的title一样的项
for (eventCursor.moveToFirst(); !eventCursor.isAfterLast(); eventCursor.moveToNext()) {
String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
if (!TextUtils.isEmpty(title) && title.equals(eventTitle)) {
int id = eventCursor.getInt(eventCursor.getColumnIndex(CalendarContract.Calendars._ID));//取得id
Uri deleteUri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), id);
int rows = context.getContentResolver().delete(deleteUri, null, null);
if (rows == -1) { //事件删除失败
return;
}
}
}
}
} finally {
if (eventCursor != null) {
eventCursor.close();
}
}
}
}