我正在使用以下代码(来自开发者网站)在设备日历中插入事件 -
long calID = 1;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2015, 9, 6, 7, 00);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2015, 9, 6, 8, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Test Title");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
TimeZone tz = TimeZone.getDefault();
values.put(Events.EVENT_TIMEZONE, tz.getID());
Uri uri = cr.insert(Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
代码似乎有效,因为我收到了插入事件的eventID。
执行此操作后,我无法在日历应用中看到该事件。我尝试从设置>帐户> google>日历同步日历。
代码中是否有错误或我要添加的任何其他代码以查看日历中的事件?
答案 0 :(得分:0)
感谢@Nobu Games,我解决了这个问题。问题是calendarId = 1与任何日历都没有关联。我创建了一种方法,使用以下代码在设备中找出可用的calendarIds -
public MyCalendar [] getCalendar(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()){
m_calendars = new MyCalendar[managedCursor.getCount()];
String calName;
String calID;
int cont= 0;
int nameCol = managedCursor.getColumnIndex(projection[1]);
int idCol = managedCursor.getColumnIndex(projection[0]);
do {
calName = managedCursor.getString(nameCol);
calID = managedCursor.getString(idCol);
m_calendars[cont] = new MyCalendar(calName, calID);
cont++;
} while(managedCursor.moveToNext());
managedCursor.close();
}
return m_calendars;
}
然后我就能看到日历中的事件了。