Create a repeating event with reminder until specific day without Intent in Android?

不问归期 提交于 2019-12-03 12:54:31
        ContentResolver cr = ctx.getContentResolver();
        ContentValues values = new ContentValues();

        values.put(CalendarContract.Events.DTSTART, dtstart);
        values.put(CalendarContract.Events.TITLE, title);
        values.put(CalendarContract.Events.DESCRIPTION, comment);

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

        // default calendar
        values.put(CalendarContract.Events.CALENDAR_ID, 1);

        values.put(CalendarContract.Events.RRULE, "FREQ=DAILY;UNTIL="
                + dtUntill);
        // for one hour
        values.put(CalendarContract.Events.DURATION, "+P1H");

        values.put(CalendarContract.Events.HAS_ALARM, 1);

        // insert event to calendar
        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

where dtuntil is

    SimpleDateFormat yyyymmdd = new SimpleDateFormat("yyyymmdd");
    Calendar dt = Calendar.getInstance();

    // where untilDate is a date instance of your choice,for example 30/01/2012
    dt.setTime(untilDate);

    // if you want the event until 30/01/2012 we add one day from our day
    // because UNTIL in RRule sets events Before the last day want for event
    dt.add(Calendar.DATE, 1);
    String dtUntill = yyyymmdd.format(dt.getTime());

    // Uri
    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

    // get the event ID that is the last element in the Uri
    long eventID = Long.parseLong(uri.getLastPathSegment());

    // add 10 minute reminder for the event
    ContentValues reminders = new ContentValues();
    reminders.put(Reminders.EVENT_ID, eventID);
    reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
    reminders.put(Reminders.MINUTES, 10);

    Uri uri = cr.insert(Reminders.CONTENT_URI, reminders);

Ref: Recurrence Rule

Here is a good Example of what you want.

Update for more information about calender and implementing reminders or other stuff see this

You can also get help from the following code

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", date);
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY"); //To set the repeat rule
intent.putExtra("endTime", date);
intent.putExtra("title", summary);

To create a time bound reoccuring event over multiple days, we need to use CalendarContract.Events.RRULE. The rule is combination frequency, count etc.

Lets say we need to create an event occuring daily for 10 days in a specific time period of day:

Intent(Intent.ACTION_INSERT)
            .setData(CalendarContract.Events.CONTENT_URI)
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                    beginCalendarTime.getTimeInMillis())
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
                    endCalendarTime.getTimeInMillis())
            .putExtra(CalendarContract.Events.TITLE, heading)
            .putExtra(CalendarContract.Events.DESCRIPTION, "To be added")
            .putExtra(CalendarContract.Events.EVENT_LOCATION, location)
            .putExtra(CalendarContract.Events.RRULE, "FREQ=DAILY;COUNT=10");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!