0
Android 앱에서 Google 캘린더에 이벤트를 생성 할 수 있어야합니다. 나는 Calendar API가 있다고 생각하지만 결코 사용한 적이 없다. 나는 안드로이드 개발에 상당히 익숙하기 때문에 이전에 탐색 한 몇 가지 예제를 발견하고 다음 코드를 사용하여 Android 캘린더를 업데이트하려고했습니다.내 Android 코드에서 캘린더를 동적으로 업데이트하는 방법
public static boolean updateCalendar(Context context,String cal_Id,String eventId)
{
try{
Uri CALENDAR_URI = Uri.parse(CAL_URI+"events");
Cursor c = context.getContentResolver().query(CALENDAR_URI, null, null, null, null);
String[] s = c.getColumnNames();
if (c.moveToFirst())
{
while (c.moveToNext())
{
String _id = c.getString(c.getColumnIndex("_id"));
String CalId = c.getString(c.getColumnIndex("calendar_id"));
if ((_id==null) && (CalId == null))
{
return false;
}
else
{
if (_id.equals(eventId) && CalId.equals(cal_Id))
{
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(_id));
context.getContentResolver().update(uri, null, null, null);// need to give your data here
return true;
}
}
}
}
}
finally
{
return true;
}
}
그러나 나는 그것을 호출되지 않는 getColumnNames를 실행하고 코드가 직선 라인 context.getContentResolver() 업데이트 (URI, NULL, NULL, NULL)로 이동합니다.; 종료합니다.
캘린더에 몇 가지 테스트 이벤트를 넣었습니다. 왜 코드를 사용하지 않는 이유가 무엇입니까?
private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}
참고 :이 이벤트를 추가 할 수
답장을 보내 주셔서 감사합니다. 코드를 애플리케이션에 삽입했지만 몇 가지 오류 메시지가 나타납니다. 두 줄의 코드 Uri EVENTS_URI = Uri.parse (getCalendarUriBase (this) + "events"); ContentResolver cr = getContentResolver(); 오류 메시지를 던지고 있는데, 처음에는 '정적 컨텍스트에서 사용할 수 없다'고하고 두 번째는 'ContextWrapper 유형에서 비 정적 메서드 getContentResolver()에 정적 참조를 만들 수 없습니다'라고 말합니다. – user616076
정적 메서드에 코드를 넣었습니까? 에뮬레이터에서 작동하지 않을 수 있습니다. – Abhi
http://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static- 이 방법을 확인하십시오. – Abhi