将每周活动添加到日历

时间:2015-03-05 07:06:39

标签: android android-intent calendar android-calendar

我想向原生Calendar添加一个活动,在这里我想在每个Tuesday重复此活动,直到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);
    }                               
}

问题:在日历中,每个Thursday都会显示此事件,而我在代码中使用了“tu

还有一件事,如果我还想给这个事件留出时间,例如:from 6:00 pm to 9:00 pm

3 个答案:

答案 0 :(得分:4)

你说这显示了周四的重复,但我得到的是周四的开始日,每周二重复一次。所以我很确定RRULE部分是对的。

我认为你所要做的就是用Calendar设置实际的开始和结束时间以获得正确的毫秒,然后用户“beginTime”而不是“dtstart”和“endTime”而不是“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);
 }

答案 1 :(得分:2)

对于星期二,首字母必须是全部资本,即。 .putExtra(CalendarContract.Events.RRULE, "FREQ=WEEKLY;BYDAY=TU;UNTIL=20151231")

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

为您需要添加的事件提供持续时间

{{1}}

您可以详细了解重复规则herehere

答案 2 :(得分:0)

在这里,我将与您分享一个简单的代码,希望能帮助您或指导您:

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));