您的位置:首页 > 移动开发 > Android开发

往系统日历中增加与删除事件提醒,Calendar Provider 实现

2017-06-02 17:09 1401 查看

Calendar Provider 日历提醒事件的添加与删除

最近需要做关于日历这块的功能,各种百度,结果千篇一律,但是获取Calender的ID时,大部分取的都是集合的第一个。记得在某些手机上测试时是有问题的,添加之后手动无法删除。最近Android开发者网站不用翻墙了,就对比看了一下,整理一下

日历提供程序官方链接

注意这段代码,测试几部国产手机,默认的日历级别都是700

以下是全部代码,没有更新的,可以对照官网(包含了权限申请,只支持4.0以上)

cur = cr.query(uri, null, CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL + ">= 700 ", null, null);


所以在查找Calender的ID时用这个条件

/**
* 检查版本是否低于Api 14   4.0
*
* @return
*/
private boolean checkVersionSdk() {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return true;
}
return false;
}
/**
* 检测是否有读取日历的权限组
*
* @return
*/
private boolean checkCalendarPermission() {
if ((ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) ||
(ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CALENDAR,Manifest.permission.WRITE_CALENDAR}, 100);
return true;
}
return false;
}

/**
* 检查是否不符合条件 true 不符合
*
* @return
*/
public boolean checkSdkPermission() {
if (checkVersionSdk()) {
return true;
}
if (checkCalendarPermission()) {
return true;
}
return false;
}

/**
* 查询日历,获取ID,用于插入事件
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void myQueryCalendar() {
long calID = -1;
try {
// Run query
if (checkSdkPermission()) {
return;
}
Cursor cur = null;
ContentResolver cr = context.getContentResolver();
Uri uri = null;
uri = CalendarContract.Calendars.CONTENT_URI;
cur = cr.query(uri, null, CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL + ">= 700 ", null, null);
while (cur.moveToNext()) {
String displayName = null;
String accountName = null;
String ownerName = null;
String accountType = null;
String visible = null;
String syncEnvents = null;

// Get the field values
calID = cur.getLong(cur.getColumnIndex(CalendarContract.Calendars._ID));
displayName = cur.getString(cur.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME));
accountName = cur.getString(cur.getColumnIndex(CalendarContract.Calendars.ACCOUNT_NAME));
ownerName = cur.getString(cur.getColumnIndex(CalendarContract.Calendars.OWNER_ACCOUNT));
accountType = cur.getString(cur.getColumnIndex(CalendarContract.Calendars.ACCOUNT_TYPE));
visible = cur.getString(cur.getColumnIndex(CalendarContract.Calendars.VISIBLE));
syncEnvents = cur.getString(cur.getColumnIndex(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL));

// Do something with the values...
Log.d("TAG", "calID:" + calID + "\ndisplayName:" + displayName + "\naccountName:" + accountName + "\nownerName:" + ownerName
+ "\naccountType:" + accountType
+ "\nvisible:" + visible
+ "\nCALENDAR_ACCESS_LEVEL:" + syncEnvents);
break;
}
if (calID < 0) {
return;
}
insertEvent(calID);
queryEvents("test");
} catch (Exception e) {
e.printStackTrace();
} finally {

}
}

/**
* 查询所有的事件,根据传入title模糊查询
* @param title
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void queryEvents(String title) {
if (checkSdkPermission()) {
return;
}
Cursor cur = null;
ContentResolver cr = context.getContentResolver();
Uri uri = null;
String selection = "(" + CalendarContract.Events.TITLE + " like ?)";
String[] selectionArgs = new String[]{"%" + title + "%"};
uri = CalendarContract.Events.CONTENT_URI;
cur = cr.query(uri, null, selection, selectionArgs, null);
long clendId = 0, eventId = 0;
String[] names = cur.getColumnNames();
while (cur.moveToNext()) {
long calID = 0;
String displayName = null;
String accountName = null;
long ownerName;
long ownerName2;
long ownerName3;

calID = cur.getLong(cur.getColumnIndex(CalendarContract.Events.CALENDAR_ID));
displayName = cur.getString(cur.getColumnIndex(CalendarContract.Events.TITLE));
accountName = cur.getString(cur.getColumnIndex(CalendarContract.Events.DESCRIPTION));
ownerName = cur.getLong(cur.getColumnIndex(CalendarContract.Events._ID));
ownerName2 = cur.getLong(cur.getColumnIndex(CalendarContract.Events.DTSTART));
ownerName3 = cur.getLong(cur.getColumnIndex(CalendarContract.Events.DTEND));
clendId = calID;
eventId = ownerName;
Log.d("TAG", "CALENDAR_ID:" + calID + "\nTITLE:" + displayName + "\nDESCRIPTION:" + accountName + "\n_ID:" + ownerName
+ "\nDTSTART:" + new Date(ownerName2) + "\nDTEND:" + new Date(ownerName3));

}
}

/**
* 插入事件到calender_ID中
* @param calID
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
void insertEvent(long calID) {
if (checkSdkPermission()) {
return;
}
long startMillis = 0;
long endMillis = 0;
//        Calendar beginTime = Calendar.getInstance();
//        Date date = new Date();
//        beginTime.setTime(date);
//        beginTime.set(date.getYear(), 6, 3, 11, 11);
//        startMillis = beginTime.getTimeInMillis();
//        Calendar endTime = Calendar.getInstance();
//        endTime.set(2017, 6, 3, 12, 11);
//        endMillis = endTime.getTimeInMillis();
startMillis = System.currentTimeMillis() + 11 * 60 * 1000;
endMillis = startMillis + 10 * 60 * 1000;
ContentResolver cr = activity.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "test");
values.put(CalendarContract.Events.DESCRIPTION, "Just For  Test");
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "Asia/Shanghai");
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
Log.e("TAG", "insert eventid:" + eventID);
insertReminders(eventID);
}

/**
* 根据事件ID删除
* @param envId
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
void deleteEventById(long envId) {
if (envId <= 0) {
return;
}
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
Uri deleteUri = null;
deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, envId);
int rows = context.getContentResolver().delete(deleteUri, null, null);
Log.i("TAG", "Rows deleted: " + rows);
}

/**
* 加入提醒
* @param eventID
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
void insertReminders(long eventID) {
if (checkSdkPermission()) {
return;
}
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.MINUTES, 10);
values.put(CalendarContract.Reminders.EVENT_ID, eventID);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 日历
相关文章推荐