我正在使用日历合同从数据库中提取事件。
首先我得到一个日历列表,然后我在每个日历的特定时间提取所有事件,一切正常。
我使用_id
列名来获取事件 ID。
但是,当我尝试让未决事件打开该特定事件的日历时,该日历会在另一个事件上打开。
我已经进行了一些测试以查看 ID 是否唯一并且看起来正确。
这是创建的pendingIntent
:
public static PendingIntent getEventPendingIntent(Context c, int eventID) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri.Builder uri = Events.CONTENT_URI.buildUpon();
uri.appendPath(Long.toString(eventID));
intent.setData(uri.build());
return PendingIntent.getActivity(c, 0, intent, 0);
}
这是获取给定 CalendarID 事件的方法:
private static Cursor getEvents(String calendarID, ContentResolver contentResolver, long timeFromNow) {
// Create a builder to define the time span
Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now);
if (timeFromNow == -1) {
timeFromNow = DateUtils.DAY_IN_MILLIS * 10000;
}
ContentUris.appendId(builder, now + timeFromNow);
Cursor eventCursor = null;
// Create an event cursor to find all events in the calendar
String[] queryFields = {"title", "dtstart", "begin", "end", "allDay", "eventLocation","description", "_id" };
try {
eventCursor = contentResolver.query(builder.build(),
queryFields , "calendar_id=" + calendarID, null, "startDay ASC, startMinute ASC");
} catch (Exception e) {
Log.w(TAG, "ERROR: "+e.toString());
Log.w(TAG, "Didn't find calender for ID="+calendarID);
eventCursor = null;
}
return eventCursor;
}