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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值