有没有可能从手机上离线获取日历条目?似乎唯一的方法就是使用gdata-java-client。
发布于 2011-02-28 06:43:57
这些答案都很好,但它们都涉及到Calendar URI的硬编码(我在不同的安卓设备上看到了三个不同的版本)。
获取该URI的更好方法(改为对类名和字段进行硬编码)如下所示:
Class<?> calendarProviderClass = Class.forName("android.provider.Calendar");
Field uriField = calendarProviderClass.getField("CONTENT_URI");
Uri calendarUri = (Uri) uriField.get(null);这并不完美(如果他们删除了android.provider.Calendar类或CONTENT_URI字段,它就会崩溃),但它比任何一个URI硬编码都适用于更多的平台。
请注意,这些反射方法将抛出exceptions,它需要被调用方法捕获或重新抛出。
发布于 2010-10-18 20:07:38
约瑟夫和艾萨克用于访问日历的解决方案只能在Android 2.1和更早版本中运行。Google已经将2.2中的基本内容URI从“content :// changed”更改为“content://com.android.endar”。此更改意味着最好的方法是尝试使用旧的基URI获取游标,如果返回的游标为空,则尝试使用新的基URI。
请注意,我从Shane Conder和Lauren Darcey在他们的Working With The Android Calendar文章中提供的open source test code中获得了这种方法。
private final static String BASE_CALENDAR_URI_PRE_2_2 = "content://calendar";
private final static String BASE_CALENDAR_URI_2_2 = "content://com.android.calendar";
/*
* Determines if we need to use a pre 2.2 calendar Uri, or a 2.2 calendar Uri, and returns the base Uri
*/
private String getCalendarUriBase() {
Uri calendars = Uri.parse(BASE_CALENDAR_URI_PRE_2_2 + "/calendars");
try {
Cursor managedCursor = managedQuery(calendars, null, null, null, null);
if (managedCursor != null) {
return BASE_CALENDAR_URI_PRE_2_2;
}
else {
calendars = Uri.parse(BASE_CALENDAR_URI_2_2 + "/calendars");
managedCursor = managedQuery(calendars, null, null, null, null);
if (managedCursor != null) {
return BASE_CALENDAR_URI_2_2;
}
}
} catch (Exception e) { /* eat any exceptions */ }
return null; // No working calendar URI found
}发布于 2009-05-13 12:47:39
您可以使用日历内容提供程序(com.android.providers.calendar.CalendarProvider)。示例:
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(Uri.parse("content://calendar/events"), null, null, null, null);
while(cursor.moveToNext()) {
String eventTitle = cursor.getString(cursor.getColumnIndex("title"));
Date eventStart = new Date(cursor.getLong(cursor.getColumnIndex("dtstart")));
// etc.
}API :您可能希望将其放在一个包装器中(请参阅Isaac's post),因为它目前是一个私有。
https://stackoverflow.com/questions/846942
复制相似问题