Martin
Martin

Reputation: 69

Android reminder intent

Is it possible to open creating reminder using intent like an event? I couldn't find any solution.

Intent intent = new Intent(Intent.ACTION_INSERT)
        .setData(CalendarContract.Events.CONTENT_URI);
startActivity(intent);

I want to invoke this:

Upvotes: 4

Views: 3155

Answers (1)

Sibidharan
Sibidharan

Reputation: 2756

You can implement it like this. All you need to do is, call this method, and it will take care of the rest.

public static void pushAppointmentsToCalender(Activity curActivity, 
                                              String title, long startDate) {

    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", startDate);
    intent.putExtra("allDay", true);
    intent.putExtra("endTime", startTime+24*60*60*1000);
    intent.putExtra(CalendarContract.Events.TITLE, title);
    curActivity.startActivity(intent);
}

But before that, make sure you have these permissions manifested.

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

If you are using Android API 23 (6.0) or above, I recommend you to read about the new permission model in API 23 here. And make sure to request in runtime, which will be good for your app to support future versions of android.

UPDATE

You should try this as suggested by Sanjeev Kumar in here

 /** Adds Events and Reminders in Calendar. */
 private void addReminderInCalendar() {
     Calendar cal = Calendar.getInstance();
     Uri EVENTS_URI = Uri.parse(getCalendarUriBase(true) + "events");
     ContentResolver cr = getContentResolver();
     TimeZone timeZone = TimeZone.getDefault();
 
     /** Inserting an event in calendar. */
     ContentValues values = new ContentValues();
     values.put(CalendarContract.Events.CALENDAR_ID, 1);
     values.put(CalendarContract.Events.TITLE, "Sanjeev Reminder 01");
     values.put(CalendarContract.Events.DESCRIPTION, "A test Reminder.");
     values.put(CalendarContract.Events.ALL_DAY, 0);
     // event starts at 11 minutes from now
     values.put(CalendarContract.Events.DTSTART, cal.getTimeInMillis() + 1 * 60 * 1000);
     // ends 60 minutes from now
     values.put(CalendarContract.Events.DTEND, cal.getTimeInMillis() + 2 * 60 * 1000);
     values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
     values.put(CalendarContract.Events.HAS_ALARM, 1);
     Uri event = cr.insert(EVENTS_URI, values);
 
     // Display event id.
     Toast.makeText(getApplicationContext(), "Event added :: ID :: " + event.getLastPathSegment(), Toast.LENGTH_SHORT).show();
 
     /** Adding reminder for event added. */
     Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(true) + "reminders");
     values = new ContentValues();
     values.put(CalendarContract.Reminders.EVENT_ID, Long.parseLong(event.getLastPathSegment()));
     values.put(CalendarContract.Reminders.METHOD, Reminders.METHOD_ALERT);
     values.put(CalendarContract.Reminders.MINUTES, 10);
     cr.insert(REMINDERS_URI, values);
 }
 
 /** Returns Calendar Base URI, supports both new and old OS. */
 private String getCalendarUriBase(boolean eventUri) {
     Uri calendarURI = null;
     try {
         if (android.os.Build.VERSION.SDK_INT <= 7) {
             calendarURI = (eventUri) ? Uri.parse("content://calendar/") :
             Uri.parse("content://calendar/calendars");
         } else {
             calendarURI = (eventUri) ? Uri.parse("content://com.android.calendar/") : Uri
                     .parse("content://com.android.calendar/calendars");
         }
     } catch (Exception e) {
         e.printStackTrace();
     }
     return calendarURI.toString();
 }

Upvotes: 1

Related Questions