事件会在几秒钟后自动从日历中删除。 这是我用来添加事件的代码。我认为这可能是由于事件的同步。搜索很多,但没有得到解决方案。
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID,3 );
java.util.TimeZone timeZone = java.util.TimeZone.getDefault();
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
event.put(CalendarContract.Events.TITLE, "DUMMY Event");
event.put(CalendarContract.Events.DESCRIPTION, "Birthday");
event.put(CalendarContract.Events.EVENT_LOCATION, "My Guest House");
event.put(CalendarContract.Events.DTSTART, ((Long.parseLong(System.currentTimeMillis()+3600000);
event.put(CalendarContract.Events.DTEND, (Long.parseLong(System.currentTimeMillis()+4200000);
event.put(CalendarContract.Events.ALL_DAY, 0);
event.put(CalendarContract.Events.HAS_ALARM, 1); event.put(CalendarContract.Events.CALENDAR_TIME_ZONE, timeZone);
Uri baseUri;
if (Build.VERSION.SDK_INT >= 8) {
baseUri = Uri.parse("content://com.android.calendar/events");
} else {
baseUri = Uri.parse("content://calendar/events");
}
Uri uri=getContentResolver().insert(baseUri, event);
//获取Gmail日历
public static String getGmailCalendarId(Context c) {
String projection[] = {"_id", "calendar_displayName"};
Uri calendars;
calendars =
Uri.parse("content://com.android.calendar/calendars");
ContentResolver contentResolver = c.getContentResolver();
Cursor managedCursor = contentResolver.query(calendars,
projection, null, null, null);
if (managedCursor.moveToFirst())
{
String calName;
String calID;
int nameCol = managedCursor.getColumnIndex(projection[1]);
int idCol = managedCursor.getColumnIndex(projection[0]);
do
{
calName = managedCursor.getString(nameCol);
calID = managedCursor.getString(idCol);
if(calName.contains("@gmail")){
calenderId = calID;
}
} while (managedCursor.moveToNext());
managedCursor.close();
return calenderId;
}
return calenderId;
}