Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nope, it is more complicated than just calling a method, if you want to transparently add it into the user's calendar.</p> <p>You've got a couple of choices;</p> <ol> <li><p><strong>Calling the intent to add an event on the calendar</strong> <br>This will pop up the Calendar application and let the user add the event. You can pass some parameters to prepopulate fields:</p> <pre><code>Calendar cal = Calendar.getInstance(); Intent intent = new Intent(Intent.ACTION_EDIT); intent.setType("vnd.android.cursor.item/event"); intent.putExtra("beginTime", cal.getTimeInMillis()); intent.putExtra("allDay", false); intent.putExtra("rrule", "FREQ=DAILY"); intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000); intent.putExtra("title", "A Test Event from android app"); startActivity(intent); </code></pre> <p>Or the more complicated one:</p></li> <li><p><strong>Get a reference to the calendar with this method</strong> <br>(It is <strong>highly</strong> recommended <strong>not</strong> to use this method, because it could break on newer Android versions):</p> <pre><code>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; } </code></pre> <p><strong>and add an event and a reminder this way</strong>:</p> <pre><code>// get calendar Calendar cal = Calendar.getInstance(); Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events"); ContentResolver cr = getContentResolver(); // event insert ContentValues values = new ContentValues(); values.put("calendar_id", 1); values.put("title", "Reminder Title"); values.put("allDay", 0); values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now values.put("description", "Reminder description"); values.put("visibility", 0); values.put("hasAlarm", 1); Uri event = cr.insert(EVENTS_URI, values); // reminder insert Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders"); values = new ContentValues(); values.put( "event_id", Long.parseLong(event.getLastPathSegment())); values.put( "method", 1 ); values.put( "minutes", 10 ); cr.insert( REMINDERS_URI, values ); </code></pre> <p>You'll also need to add these permissions to your manifest for this method:</p> <pre><code>&lt;uses-permission android:name="android.permission.READ_CALENDAR" /&gt; &lt;uses-permission android:name="android.permission.WRITE_CALENDAR" /&gt; </code></pre></li> </ol> <p><strong>Update: ICS Issues</strong></p> <p>The above examples use the undocumented Calendar APIs, new public Calendar APIs have been released for ICS, so for this reason, to target new android versions you should use <a href="http://developer.android.com/reference/android/provider/CalendarContract.html">CalendarContract</a>.</p> <p>More infos about this can be found at <a href="http://android-developers.blogspot.co.uk/2011/10/ics-and-non-public-apis.html">this blog post</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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