获取与修改android自带日历日程

Android中的日历,只有真机才有,模拟上是没有的,所以测试环境一定要真机!!

因为日历是系统自带的,所以我们读写它一定要申请权限,也就是在AndroidManifest.xml加如下两行代码(一个读一个写):

<!-- get calendar read and write permission -->
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
日历 日程URL在2.2以后版本有所改变

// 为了兼容不同版本的日历,2.2以后url发生改变
	private static String calanderEventURL = "";
	static {
		if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
			calanderEventURL = "content://com.android.calendar/events";
		} else {
			calanderEventURL = "content://calendar/events";
		}

	}
下面是获取日程的主要代码:
ContentResolver cr = context.getContentResolver();
		// 日历里面相应的Event的URI
		Uri uri = Uri.parse(calanderEventURL);
		Cursor cursor = cr.query(uri, null, null, null, null);
		while (cursor.moveToNext()) {
			CalendarEvent cde = new CalendarEvent();
			cde.setCalendar_id(cursor.getString(cursor
					.getColumnIndex(Events.CALENDAR_ID)));
			cde.setTitle(cursor.getString(cursor.getColumnIndex(Events.TITLE)));
			cde.setDtstart(cursor.getString(cursor
					.getColumnIndex(Events.DTSTART)));
			cde.setDtend(cursor.getString(cursor.getColumnIndex(Events.DTEND)));
			cde.setDuration(cursor.getString(cursor
					.getColumnIndex(Events.DURATION)));
			cde.setRrule(cursor.getString(cursor.getColumnIndex(Events.RRULE)));
			cde.setEventTimezone(cursor.getString(cursor
					.getColumnIndex(Events.EVENT_TIMEZONE)));
			calendarEvents.add(cde);
		}
下面是写入日程的主要代码

写入的时候这几个字段是必须的所以我就只写了这几个字段了

ContentResolver cr = context.getContentResolver();
		// 日历里面相应的Event的URI
		Uri uri = Uri.parse(calanderEventURL);
		Cursor cursor = cr.query(uri, null, null, null, null);
		while (cursor.moveToNext()) {
			CalendarEvent cde = new CalendarEvent();
			cde.setCalendar_id(cursor.getString(cursor
					.getColumnIndex(Events.CALENDAR_ID)));
			cde.setTitle(cursor.getString(cursor.getColumnIndex(Events.TITLE)));
			cde.setDtstart(cursor.getString(cursor
					.getColumnIndex(Events.DTSTART)));
			cde.setDtend(cursor.getString(cursor.getColumnIndex(Events.DTEND)));
			cde.setDuration(cursor.getString(cursor
					.getColumnIndex(Events.DURATION)));
			cde.setRrule(cursor.getString(cursor.getColumnIndex(Events.RRULE)));
			cde.setEventTimezone(cursor.getString(cursor
					.getColumnIndex(Events.EVENT_TIMEZONE)));
			calendarEvents.add(cde);
		}



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值