android 调用系统日历 插入日历行程或工作行程

//先定义一个URL,到时作为调用系统日历的uri的参数
String calanderRemiderURL = "";
if (Build.VERSION.SDK_INT >= 8) {
    calanderRemiderURL = "content://com.android.calendar/reminders";
} else {
    calanderRemiderURL = "content://calendar/reminders";
}

long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2018, 0, 31, 12, 05);  //注意,月份的下标是从0开始的
startMillis = beginTime.getTimeInMillis();  //插入日历时要取毫秒计时
Calendar endTime = Calendar.getInstance();
endTime.set(2018, 0, 31, 12, 50);
endMillis = endTime.getTimeInMillis();

ContentValues eValues = new ContentValues();  //插入事件
ContentValues rValues = new ContentValues();  //插入提醒,与事件配合起来才有效
TimeZone tz = TimeZone.getDefault();//获取默认时区

//插入日程
eValues.put(CalendarContract.Events.DTSTART, startMillis);
eValues.put(CalendarContract.Events.DTEND, endMillis);
eValues.put(CalendarContract.Events.TITLE, "qwqqe师");
eValues.put(CalendarContract.Events.DESCRIPTION, "去实验室见研究生导师");
eValues.put(CalendarContract.Events.CALENDAR_ID, calID);
eValues.put(CalendarContract.Events.EVENT_LOCATION, "计算机学院");
eValues.put(CalendarContract.Events.EVENT_TIMEZONE, tz.getID());
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

    return;
}
Uri uri = getContentResolver().insert(CalendarContract.Events.CONTENT_URI, eValues);

//插完日程之后必须再插入以下代码段才能实现提醒功能
String myEventsId = uri.getLastPathSegment(); // 得到当前表的_id
rValues.put("event_id", myEventsId);
rValues.put("minutes", 10); //提前10分钟提醒
rValues.put("method", 1);   //如果需要有提醒,必须要有这一行
getContentResolver().insert(Uri.parse(calanderRemiderURL), rValues);

猜你喜欢

转载自blog.csdn.net/mr___xu/article/details/79219456