18

I would like to add an event to native Calendar, here i want to repeat this event on every Tuesday until 31 December 2015:

btnWeekly.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {       
        Calendar calendar = Calendar.getInstance(); 

        Intent intent = new Intent(Intent.ACTION_INSERT)
                .setData(Events.CONTENT_URI)
                .setType("vnd.android.cursor.item/event")
                .putExtra(Events.TITLE, "Tuesdays")
                .putExtra(Events.DESCRIPTION, "Tuesday Specials")
                .putExtra(Events.EVENT_LOCATION, "Lixious Bench")
                .putExtra(Events.RRULE, "FREQ=WEEKLY;BYDAY=Tu;UNTIL=20151231")
                .putExtra(Events.DTSTART, calendar.getTimeInMillis())
                .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true)
                .putExtra(CalendarContract.Events.HAS_ALARM, 1)
                .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);
        startActivity(intent);
    }                               
}

Problem: In Calendar it showing this event for every Thursday, whereas i have used "tu" in my code

And one more thing what if i also want to give time duration for this event like: from 6:00 pm to 9:00 pm only.

Tyler Carberry
  • 2,001
  • 1
  • 14
  • 10
Sun
  • 6,768
  • 25
  • 76
  • 131

3 Answers3

4

You said it showed repeating for Thursday, but what I got was a start day of Thursday with a repeat every Tuesday. So I'm pretty sure the RRULE part is right.

I think all you have to do is set the actual start and end times with Calendar to get the right milliseconds, then user "beginTime" instead of "dtstart" and "endTime" instead of "dtend".

@Override
public void onClick(View v) {

    // If you want the start times to show up, you have to set them
    Calendar calendar = Calendar.getInstance();

    // Here we set a start time of Tuesday the 17th, 6pm
    calendar.set(2015, Calendar.MARCH, 17, 18, 0, 0);
    calendar.setTimeZone(TimeZone.getDefault());

    long start = calendar.getTimeInMillis();
    // add three hours in milliseconds to get end time of 9pm
    long end = calendar.getTimeInMillis() + 3 * 60 * 60 * 1000;

    Intent intent = new Intent(Intent.ACTION_INSERT)
            .setData(Events.CONTENT_URI)
            .setType("vnd.android.cursor.item/event")
            .putExtra(Events.TITLE, "Tuesdays")
            .putExtra(Events.DESCRIPTION, "Tuesday Specials")
            .putExtra(Events.EVENT_LOCATION, "Lixious Bench")
            .putExtra(Events.RRULE, "FREQ=WEEKLY;BYDAY=TU;UNTIL=20150428")

            // to specify start time use "beginTime" instead of "dtstart"
            //.putExtra(Events.DTSTART, calendar.getTimeInMillis())
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start)
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end)

            // if you want to go from 6pm to 9pm, don't specify all day
            //.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true)
            .putExtra(CalendarContract.Events.HAS_ALARM, 1)
            .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);

    startActivity(intent);
 }
Tyler Carberry
  • 2,001
  • 1
  • 14
  • 10
kris larson
  • 30,387
  • 5
  • 62
  • 74
  • And the RRULE you have right now should work right for that. You're just setting DTSTART to match the Tuesday part. Try it out! – kris larson Mar 09 '15 at 06:59
  • Forgot a comma at the end; I typed it in manually. See updated answer. – kris larson Mar 09 '15 at 14:43
  • Yes, just got it straightened out. The intent key was wrong for start time and end time. See my updated answer. – kris larson Mar 13 '15 at 03:27
  • hello, thank you so much for your solution, i just tried your way and i would like to say you are the master :) but still one issue I did not get any notification about event... no doubt event has been inserted successfully, So what we are missing ? Why we are not getting Notification about event... – Sun Jul 01 '15 at 13:07
  • Answer from @parik-dhakan has some code for AlarmManager, I think that's what you want. Apparently the HAS_ALARM column is only meant to indicate whether an alarm was set or not; you have to do the alarm-setting yourself. Cheers – kris larson Jul 01 '15 at 13:40
2

For Tuesday the Initials have to be all capital ie. TU

.putExtra(CalendarContract.Events.RRULE, "FREQ=WEEKLY;BYDAY=TU;UNTIL=20151231")

To give time duration for the event you need to add

.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,getMillis(begintime))
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, getMillis(endtime))

You can learn more about recurrance rule here and here

lil'ms
  • 452
  • 3
  • 5
  • 17
0

Here I am sharing you a simple code hope that will help you or guide you:

Intent intentAlarm = new Intent(getActivity(), AlarmReceiver.class);

intentAlarm.putExtra("name", data.getName());
intentAlarm.putExtra("desc", data.getDescription());
intentAlarm.setData(Uri.parse("custom://" + data.getId()));
intentAlarm.setAction(String.valueOf(data.getId()));

// Create the AlarmManager
AlarmManager alarmManager = (AlarmManager) getActivity()
        .getSystemService(Context.ALARM_SERVICE);

// Set the alarm for a particular time
alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar_Object
        .getTimeInMillis(), PendingIntent.getBroadcast(
                    getActivity(), Integer.parseInt(data.getId()), intentAlarm,
                    PendingIntent.FLAG_UPDATE_CURRENT));
Tyler Carberry
  • 2,001
  • 1
  • 14
  • 10
parik dhakan
  • 787
  • 4
  • 19