2011-12-08 2 views
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; 
      } 

참고 :이 이벤트를 추가 할 수

답변

0

사용이 특정 날짜와 시간

Uri event1; 
    long epoch; 
    long epoch1; 


Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events"); 
       ContentResolver cr = getContentResolver(); 

       ContentValues values = new ContentValues(); 

       try 
       { 
        epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm").parse(YourStartDate+" "+YourStratTime).getTime(); 
        //epoch=epoch; 
        Log.e("epoch",String.valueOf(epoch)); 
        epoch1 = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm").parse(YourStartDate+" "+YourEndDate).getTime(); 
        //epoch1=epoch1; 
        Log.e("epoch1",String.valueOf(epoch1)); 
       } catch (ParseException e) 
       { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       values.put("calendar_id", 1); 
       values.put("title", "Appoitment"); 
       values.put("allDay", 0); 
       values.put("dtstart",epoch); // event starts at 11 minutes from now 
       values.put("dtend", epoch1); // ends 60 minutes from now 
       values.put("description", "Your consulting date and time "); 
       values.put("visibility", 0); 
       values.put("hasAlarm", 1); 
       if(EVENTS_URI!=null) 
       { 
       event1 = cr.insert(EVENTS_URI, values); 
       } 

       // reminder insert 
       Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders"); 
       values = new ContentValues(); 
       values.put("event_id", Long.parseLong(event1.getLastPathSegment())); 
       values.put("method", 1); 
       values.put("minutes", 10); 
       if(REMINDERS_URI!=null) 
       { 
       cr.insert(REMINDERS_URI, values); 
       } 

getCalendarUroBase 기능으로, 캘린더 YYYY-MM-DD에 있어야 샘플 날짜 당으로, 시간이해야 hh : mm 형식이 되십시오.

+0

답장을 보내 주셔서 감사합니다. 코드를 애플리케이션에 삽입했지만 몇 가지 오류 메시지가 나타납니다. 두 줄의 코드 Uri EVENTS_URI = Uri.parse (getCalendarUriBase (this) + "events"); ContentResolver cr = getContentResolver(); 오류 메시지를 던지고 있는데, 처음에는 '정적 컨텍스트에서 사용할 수 없다'고하고 두 번째는 'ContextWrapper 유형에서 비 정적 메서드 getContentResolver()에 정적 참조를 만들 수 없습니다'라고 말합니다. – user616076

+0

정적 메서드에 코드를 넣었습니까? 에뮬레이터에서 작동하지 않을 수 있습니다. – Abhi

+0

http://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static- 이 방법을 확인하십시오. – Abhi

관련 문제