Android/Calendar

Retired DISLab
이동: 둘러보기, 찾기

Developer's Guide

Event 삽입

Here's code that will let you add an event directly:

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.CalendarContract;
 
import java.util.Calendar;
 
// Construct event details
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
 
// Insert Event
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Walk The Dog");
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
values.put(CalendarContract.Events.CALENDAR_ID, 3);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
 
// Retrieve ID for new event
String eventID = uri.getLastPathSegment();


You'll need the CALENDAR_ID, so here's how to query the list of calendars:

Uri uri = CalendarContract.Calendars.CONTENT_URI;
String[] projection = new String[] {
       CalendarContract.Calendars._ID,
       CalendarContract.Calendars.ACCOUNT_NAME,
       CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
       CalendarContract.Calendars.NAME,
       CalendarContract.Calendars.CALENDAR_COLOR
};
 
Cursor calendarCursor = managedQuery(uri, projection, null, null, null);

You'll need to request the android.permission.READ_CALENDAR permission for all of this to work.

If you want to avoid having to request permissions, you can also ask the Calendar app to create a new event on your behalf by using an Intent:

Intent intent = new Intent(Intent.ACTION_INSERT)
         .setType("vnd.android.cursor.item/event")
         .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
         .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
         .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , false) // just included for completeness
         .putExtra(Events.TITLE, "My Awesome Event")
         .putExtra(Events.DESCRIPTION, "Heading out with friends to do something awesome.")
         .putExtra(Events.EVENT_LOCATION, "Earth")
         .putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10") 
         .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
         .putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE)
         .putExtra(Intent.EXTRA_EMAIL, "my.friend@example.com");
startActivity(intent);

Will this code work if i have created my app using api level 8 – Anshuman Mar 5 at 5:55 @Anshuman: No. The public calendar API is new for API level 14. – Sparky Apr 11 at 9:22 @Durai states, EVENT_TIMEZONE must also be added to ContentValues – Justin Muller Jul 13 at 22:11

TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());

Calendar Read

Cursor cursor = cr.query(Uri.parse("content://calendar/events"), 
                       new String[]{ "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" },
                       null, null, null);         
//Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "name" }, null, null, null);
String add = null;
String[] calNames = new String[cursor.getCount()];
int[] calIds = new int[cursor.getCount()];
 
cursor.moveToFirst();
 
for (int i = 0; i < calNames.length; i++) {
    calIds[i] = cursor.getInt(0);
    calNames[i] = "Event" + cursor.getInt(0) + ": \nTitle: " + cursor.getString(1) + "\nDescription: "
                + cursor.getString(2) + "\nStart Date: " + new Date(cursor.getLong(3))
                + "\nEnd Date : " + new Date(cursor.getLong(4)) + "\nLocation : " + cursor.getString(5);
    if (add == null) {
        add = calNames[i];
    } else {
        add += calNames[i];
    }           
    ((TextView)findViewById(R.id.calendars)).setText(add);
 
    cursor.moveToNext();
}
cursor.close();

For Android 2.2 and above you should use the following URI.

Uri.parse("content://com.android.calendar/events")
개인 도구
이름공간
변수
행위
둘러보기
구성원
연구
연구실
기타
도구모음
인쇄/내보내기