Android读取日历事件
我试图创建一个界面来选择calenar事件,但我遇到了一些问题。在我的设备上,我获得了正确的事件,但我的许多用户都报告该功能不起作用,有些显示的事件甚至不在日历中。在我测试的4.1三星设备上,应用程序崩溃了,所以我想知道的是;显示所有日历事件列表的正确方式是什么?下面是我现在使用的代码:Android读取日历事件,android,calendar,android-contentprovider,Android,Calendar,Android Contentprovider,我试图创建一个界面来选择calenar事件,但我遇到了一些问题。在我的设备上,我获得了正确的事件,但我的许多用户都报告该功能不起作用,有些显示的事件甚至不在日历中。在我测试的4.1三星设备上,应用程序崩溃了,所以我想知道的是;显示所有日历事件列表的正确方式是什么?下面是我现在使用的代码: Cursor cursor = getActivity().getContentResolver().query(Uri.parse ("content://com.android.calendar/even
Cursor cursor = getActivity().getContentResolver().query(Uri.parse
("content://com.android.calendar/events"),
new String[]{"calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation"}, "calendar_id=1", null, null);
cursor.moveToFirst();
int length = cursor.getCount();
for (int i = 0; i < length; i++) {
CalendarEvent ce = new CalendarEvent();
ce.setTitle(cursor.getString(1));
ce.setNote(cursor.getString(2));
ce.setDate(cursor.getLong(3));
if (cursor.getString(1).length() > 0) {
mItems.add(ce);
}
cursor.moveToNext();
}
cursor.close();
Cursor Cursor=getActivity().getContentResolver().query(Uri.parse
("content://com.android.calendar/events"),
新字符串[]{“日历id”、“标题”、“说明”,
“dtstart”、“dtend”、“eventLocation”},“calendar\u id=1”、null、null);
cursor.moveToFirst();
int length=cursor.getCount();
for(int i=0;i0){
添加(ce);
}
cursor.moveToNext();
}
cursor.close();
我怀疑罪魁祸首是这种情况:日历id=1
一台设备可以有多个日历。不能保证id=1的日历是主日历(即使您使用了主日历,用户也可能在多个不同的日历中有事件,例如个人日历和工作日历)
所以,这取决于你到底想做什么。你应该:
- 使用主日历(带有,但请参见上面的警告),或
- 让用户选择一个日历来使用您的应用程序(查询),然后从那时起使用该
,或者日历\u id
- 如果要从所有日历中获取事件,请删除
过滤器calendar\u id