往系统日历中增加与删除事件提醒,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); }
相关文章推荐
- 在app未开启时,实现对用户进行通知提醒功能总结(添加事件到手机系统日历并设置闹钟提醒)
- 在app未开启时,实现对用户进行通知提醒功能总结(添加事件到手机系统日历并设置闹钟提醒)
- 通过UICollectionView 自定义日历(Calendar)添加、编辑以及删除事件等操作
- JS中简单的删除增加鼠标事件的实现
- boss直聘把面试提醒写入系统日历事件中
- 对系统日历CalendarProvider进行增删改查操作的测试
- vue.js--基础 事件结合双向数据绑定实现todolist,增加和删除功能
- Android 弹出系统日历,添加提醒事件
- 【Windows】Windows 8.1 系统下控制台命令SET实现环境变量的增加、修改、删除方法
- android自定义日历实现事件提醒
- c语言实现的货物管理系统实例代码(增加删除 查找货物信息等功能)
- 创建一个学生系统,实现一下功能; * 1:增加学生 * 2:显示学生 * 3:修改学生 * 4:删除学生 * 5:查找学生 * 6:退出程序 * 请输入选择(
- RecyclerView的Item点击事件,增加删除Item瀑布流动画效果,长按拖动Item,RecyclerView复杂布局、实现新闻频道选择器
- android 读写系统日历、插入事件、添加多个提醒的问题
- RecyclerView 初次体验,实现增加删除条目及item长按和点击事件
- Android向系统日历中添加日程事件(一种闹钟实现)
- iOS开发将App发布的事件直接添加到系统日历提醒事项的封装
- shell实现增加删除Linux系统用户脚本(密码为随机)
- Android Calendar Provider(日历事件)
- c语言使用链表编写一个可以实现班级学生管理系统,增加,删除,修改学生信息