GData-Java-Client를 사용하지 않고 달력의 항목에 액세스 할 수있는 방법이 있습니까?
-
21-08-2019 - |
문제
전화 오프라인에서 캘린더의 항목을 얻을 수 있습니까? 유일한 방법은 사용하는 것 같습니다 gdata-java-client.
해결책
이 답변은 좋지만 모두 하드 코딩을 포함합니다. 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
호출 방법에 의해 잡히거나 다시 던져야합니다.
다른 팁
캘린더에 액세스하기위한 Josef와 Isaac의 솔루션은 Android 2.1 이상에서만 작동합니다. Google은 2.2의 기본 컨텐츠 URI를 "Content : // Calendar"에서 "Content : //com.android.calendar"로 변경했습니다. 이 변경 사항은 최선의 접근 방식은 이전베이스 URI를 사용하여 커서를 얻는 것입니다. 반환 된 커서가 NULL 인 경우 새베이스 URI를 사용해보십시오.
이 접근법을 얻었습니다 오픈 소스 테스트 코드 그 셰인 콘더 (Shane Conder)와 로렌 다세 (Lauren Darcey)는 그들의 것을 제공합니다 안드로이드 캘린더와 협력합니다 기사.
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
}
현재 개인 API를 사용하지 않으면 불가능합니다 (Josef의 게시물 참조) 캘린더 제공 업체가 있지만 아직 공개되지는 않습니다. 언제든지 변경하고 앱을 끊을 수 있습니다.
그러나 아마도 변하지 않을 것입니다 (나는 그들이 "캘린더"에서 변경할 것이라고 생각하지 않으므로 사용할 수 있습니다. 그러나 내 추천은 다음과 같은 별도의 클래스를 사용하는 것입니다.
public class CalendarProvider {
public static final Uri CONTENT_URI = Uri.parse("content://calendar");
public static final String TITLE = "title";
public static final String ....
문자열 대신 직접 사용하십시오. 이렇게하면 API가 변경되거나 공개 될 때 쉽게 변경할 수 있습니다.
캘린더 컨텐츠 제공 업체를 사용할 수 있습니다 (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입니다.
여기에서 CalendarContract를 사용할 수 있습니다. https://github.com/dschuermann/android-calendar-cationability
Android 4에서 사용 가능한 것과 동일한 API 클래스이지만 Android> = 2.2에서 작업하도록 만들어졌습니다.
변경할 수있는 API에 대해 ... 전체 ContentProvider 접근 방식이 빠르게 변경되지 않으므로 문자열 만 업데이트하여 이미 많은 문제를 극복 할 수 있습니다. 따라서 전체 프로젝트에 걸쳐 재사용 상수를 만들 수 있습니다.
public static final String URI_CONTENT_CALENDAR_EVENTS = "content://calendar/events";
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(Uri.parse(URI_CONTENT_CALENDAR_EVENTS), null, null, null, null);
//etc
적절한 개인 API를 원한다면 Pojo와 다음과 같은 서비스를 만들어야합니다.
public class CalendarEvent {
private long id;
private long date;
//etc...
}
public interface CalendarService {
public Set<CalendarEvent> getAllCalendarEvents();
public CalendarEvent findCalendarEventById(long id);
public CalendarEvent findCalendarEventByDate(long date);
}
등등. 이렇게하면 API가 변경되는 경우 캘린더 벤트 객체 와이 서비스 만 업데이트하면됩니다.
Nick의 솔루션에는 컨텍스트 클래스에서 정의되지 않은 ManagedQuery가 포함됩니다. 배경에서 물건을 실행할 때 여러 번 컨텍스트 객체를 사용하고 싶을 것입니다. 수정 된 버전은 다음과 같습니다.
공개 문자열 getCalendaruribase () {
return (android.os.build.version.sdk_int> = 8)? "내용 : //com.android.calendar": "Content : // 캘린더"; }
ManagedQuery가 이전에 성공하더라도 더 많은 예외가있을 수 있으므로 Null에 대한 캐치는 여기에서 수행해서는 안됩니다.