必须权限

    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />

 

Calendar Event Url

	private static String calanderURL = "";
	private static String calanderEventURL = "";
	private static String calanderRemiderURL = "";

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

		} else {
			calanderURL = "content://calendar/calendars";
			calanderEventURL = "content://calendar/events";
			calanderRemiderURL = "content://calendar/reminders";
		}
	}

获取用户

		// Get User Name
		Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null, null, null, null);
		if (userCursor.getCount() > 0) {
			while (userCursor.moveToNext()) {
				userCursor.getColumnNames();// [allowedAvailability, cal_sync9,
											// cal_sync8, ownerAccount,
											// cal_sync7, visible, cal_sync6,
											// canOrganizerRespond, cal_sync5,
											// cal_sync4, cal_sync3, cal_sync2,
											// canModifyTimeZone, cal_sync1,
											// calendar_color_index, _id,
											// calendar_timezone, _sync_id,
											// allowedReminders, cal_sync10,
											// name, sync_events,
											// calendar_access_level, dirty,
											// allowedAttendeeTypes,
											// canPartiallyUpdate, account_type,
											// COALESCE(isPrimary, ownerAccount
											// = account_name), maxReminders,
											// deleted, calendar_color,
											// account_name, calendar_location,
											// calendar_displayName, mutators]
				String userName = userCursor.getString(userCursor.getColumnIndex("name"));
				String calId = userCursor.getString(userCursor.getColumnIndex("_id"));
				Toast.makeText(CalendarActivity.this,calId+"    " +userName, Toast.LENGTH_LONG).show();
			}
		}

读取Calendar Event

//Get Calander Event
		Cursor eventCursor = getContentResolver().query(Uri.parse(calanderEventURL), null, null, null, null);
		if (eventCursor.getCount() > 0) {
			while (eventCursor.moveToNext()) {
				for (int i = 0; i < eventCursor.getColumnCount(); i++) {
					eventCursor.getColumnNames();// [eventEndTimezone,
													// isOrganizer, cal_sync9,
													// cal_sync8, cal_sync7,
													// visible, cal_sync6,
													// cal_sync5, rrule,
													// cal_sync4, cal_sync3,
													// cal_sync2, cal_sync1,
													// calendar_color_index,
													// hasAlarm, rdate, dtstart,
													// sync_data1, sync_data2,
													// hasAttendeeData,
													// sync_data3,
													// calendar_timezone,
													// sync_data4, description,
													// sync_data5, sync_data6,
													// calendar_access_level,
													// lunarRrule, sync_data7,
													// sync_data8, sync_data9,
													// customAppPackage,
													// modifyTime, account_type,
													// hasExtendedProperties,
													// eventLocation, dtend,
													// allDay, sync_data10,
													// organizer, lastSynced,
													// deleted, account_name,
													// originalInstanceTime,
													// mutators,
													// selfAttendeeStatus,
													// eventColor_index,
													// eventTimezone,
													// createTime,
													// allowedAvailability,
													// ownerAccount,
													// canOrganizerRespond,
													// canModifyTimeZone,
													// lastDate,
													// guestsCanModify,
													// guestsCanSeeGuests,
													// exrule, title, _id,
													// displayColor, _sync_id,
													// allowedReminders,
													// uid2445, cal_sync10,
													// dirty,
													// allowedAttendeeTypes,
													// availability,
													// calendar_id, original_id,
													// customAppUri,
													// originalAllDay,
													// maxReminders, isLunar,
													// accessLevel,
													// calendar_color, duration,
													// calendar_displayName,
													// guestsCanInviteOthers,
													// original_sync_id,
													// eventColor, exdate,
													// eventStatus]
					String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
//					Toast.makeText(CalendarActivity.this, eventTitle, Toast.LENGTH_LONG).show();
				}
			}
		}

插入 Calendar Event

		//insert Calander Event

		// 获取要出入的gmail账户的id
		String calId = "";
		userCursor = getContentResolver().query(Uri.parse(calanderURL), null, null, null, null);
		if (userCursor.getCount() > 0) {
			userCursor.moveToFirst();
			calId = userCursor.getString(userCursor.getColumnIndex("_id"));

		}
		ContentValues event = new ContentValues();
		event.put("title", "奥巴马任总统后首次回祖籍国 肯尼亚人:别空手来");
		event.put("description", "奥巴马是美国第一位非洲裔总统,他的父亲老巴拉克·奥巴马是肯尼亚的经济学家。在奥巴马21岁的时候,老奥巴马不幸遭遇车祸去世,死后葬在故乡科盖洛(Kogelo)。奥巴马曾访问过这里,并在自己的回忆录《来自我父亲的梦想》里提及此事。在他入主白宫前,当地的小学还被命名为“参议员巴拉克·奥巴马小学”。奥巴马小学目前破旧不堪,孩子们还在用破桌烂凳上课。在接受媒体采访时,该小学的一位教师说:“我们认为,美国政府至少要带一些援助过来。”");
		// 插入hoohbood@gmail.com这个账户
		event.put("calendar_id", calId);

		Calendar mCalendar = Calendar.getInstance();
		mCalendar.add(Calendar.DATE, 1);	 
		long start = mCalendar.getTime().getTime();
		mCalendar.add(Calendar.HOUR_OF_DAY, 2);
		long end = mCalendar.getTime().getTime();

		event.put("dtstart", start);
		event.put("dtend", end);
		event.put("hasAlarm", 1);
		event.put("eventTimezone", TimeZone.getDefault().getID());

		Uri newEvent = getContentResolver().insert(Uri.parse(calanderEventURL), event);
		long id = Long.parseLong(newEvent.getLastPathSegment());
		ContentValues values = new ContentValues();
		values.put("event_id", id);
		// 提前10分钟有提醒
		values.put("minutes", 10);
		getContentResolver().insert(Uri.parse(calanderRemiderURL), values);
		Toast.makeText(CalendarActivity.this, "插入事件成功!!!", Toast.LENGTH_LONG).show();

  

  

  

  

  

posted on 2015-07-21 14:21  QZB  阅读(392)  评论(0编辑  收藏  举报