안드로이드에는 일정을 관리 하기위해 내부적으로 구현이 되어 있다. 이런 내부적인 일정과 개발하려는 어플리케이션과 통합, 즉 호환되기 위해 어떻게 사용해야 하는지에 대한 몇가지 방법을 쓸까 한다.
1. 간단한 일정 등록및 수정은 Intent를 통해 앱을 실행하여 작업을 할 수 있다.
Intent intent = new Intent(Intent.ACTION_EDIT); intent.setType("vnd.android.cursor.item/event"); intent.putExtra("beginTime", stdate); intent.putExtra("allDay", false); intent.putExtra("endTime", enddate); intent.putExtra("title", title_name); intent.putExtra("description", ""); intent.putExtra("eventLocation", location); startActivity(intent);
2. 위의 1번을 통해 들어온 일정을 등록/수정하기 위해 ContentValues를 통해 직접 구현한다.
일정 추가
Uri eventUriString; if(Build.VERSION.SDK_INT >= 8) eventUriString = Uri.parse("content://com.android.calendar/events"); else eventUriString = Uri.parse("content://calendar/events"); ContentValues eventValues = new ContentValues(); eventValues.put("calendar_id", 1); // id, We need to choose from our mobile for primary its 1 eventValues.put("title", title); eventValues.put("description", info); eventValues.put("eventLocation", place); eventValues.put("dtstart", startDate); eventValues.put("dtend", endDate); eventValues.put("eventTimezone", TimeZone.getDefault().getID()); eventValues.put("eventStatus", 1); // This information is sufficient for most entries tentative (0), confirmed (1) or canceled (2): eventValues.put("hasAlarm", 1); // 0 for false, 1 for true Uri eventUri = getContentResolver().insert(eventUriString, eventValues); //to get the last inserted event id long eventID = Long.parseLong(eventUri.getLastPathSegment());
일정 삭제
Uri eventUriString; if(Build.VERSION.SDK_INT >= 8) eventUriString = Uri.parse("content://com.android.calendar/events"); else eventUriString = Uri.parse("content://calendar/events"); Uri deleteUri = ContentUris.withAppendedId(eventUriString, id); int rows = getContentResolver().delete(deleteUri, null, null);
위의 코드를 통해 내부 일정 DB에 직접 추가/삭제 한다.
단, 매니페스트의 android.permission.WRITE_CALENDAR와 android.permission.READ_CALENDA이 필요 하다.
그리고 API Level 14부터 CalendarContract Provider를 지원하기 때문에 좀 더 쉽게 구현이 가능하다.
'안드로이드 개발' 카테고리의 다른 글
구글에서 말하는 안드로이드 앱 개발 성능 개선 팁 (0) | 2013.11.25 |
---|---|
안드로이드 4.4 킷캣(KitKat) 주요 변경/추가 사항정리 (0) | 2013.11.01 |
Chrome과 Android Custom Scheme (0) | 2013.10.30 |