Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's code that will let you add an event directly:</p> <pre><code>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(); TimeZone timeZone = TimeZone.getDefault(); values.put(CalendarContract.Events.DTSTART, startMillis); values.put(CalendarContract.Events.DTEND, endMillis); values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); 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(); </code></pre> <p>You'll need the CALENDAR_ID, so here's how to query the list of calendars:</p> <pre><code>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); </code></pre> <p>You'll need to request the <code>android.permission.READ_CALENDAR</code> permission for all of this to work.</p> <p>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 <code>Intent</code>:</p> <pre><code>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); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload