创建定期日历事件 - 设置开始日期的问题

时间:2017-09-13 09:37:41

标签: android calendar

我尝试设置的活动的开始日期存在问题。尽管我定义了DTSTART,例如我们是9/13/17 11:05 am,但事件显示日期为9/13/17上午11:30至下午12:30。重复参数是正确的。

我试图在每个月的第一天下午6点设置一个活动。例如:我们是9月13日。我想从10月1日开始,然后每个月1日开始。

我的代码出了什么问题?

    long startMillis = 0;
    long endMillis = 0;
    Calendar beginTime = Calendar.getInstance();
    beginTime.set(2017, 10, 1, 18, 00);
    startMillis = beginTime.getTimeInMillis();
    Calendar endTime = Calendar.getInstance();
    endTime.set(2017, 10, 1, 18, 15);
    endMillis = endTime.getTimeInMillis();

    Intent calIntent = new Intent(Intent.ACTION_INSERT);
    calIntent.setType("vnd.android.cursor.item/event");
    calIntent.putExtra(CalendarContract.Events.TITLE, "Monthly Change");
    calIntent.putExtra(CalendarContract.Events.DESCRIPTION, "test");
    GregorianCalendar calDate = new GregorianCalendar(2014, 10, 1);
    calIntent.putExtra(CalendarContract.Events.EVENT_TIMEZONE, timezone);
    calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false);
    calIntent.putExtra(CalendarContract.Events.RRULE, "FREQ=MONTHLY");
    calIntent.putExtra(CalendarContract.Events.RDATE, startMillis);
    calIntent.putExtra(CalendarContract.Events.DTSTART, startMillis);
    calIntent.putExtra(CalendarContract.Events.DTEND, endMillis);

    startActivity(calIntent);

1 个答案:

答案 0 :(得分:0)

我通过定义开始时间和持续时间找到了解决方案:

   Uri eventsUri;
    if (android.os.Build.VERSION.SDK_INT <= 7) {
        eventsUri = Uri.parse("content://calendar/events");
    } else {
        eventsUri = Uri.parse("content://com.android.calendar/events");
    }
    Calendar cal = Calendar.getInstance();
    ContentValues event = new ContentValues();
    event.put("calendar_id", 1);
    event.put("title", "Event Title");
    event.put("description", "Event Desc");
    event.put("eventLocation", "Event Location");
    cal.set(2017, 9, 1, 14, 0);
    event.put("dtstart", cal.getTimeInMillis());
    event.put(CalendarContract.Reminders.DTSTART, cal.getTimeInMillis());
    event.put("rrule", "FREQ=MONTHLY");
    event.put("allDay", 0);   // 0 for false, 1 for true
    event.put("eventStatus", 1);
    event.put("hasAlarm", 1); // 0 for false, 1 for true
    event.put("duration","P900S");
    event.put("eventTimezone", TimeZone.getDefault().getID());
    Uri url = getActivity().getContentResolver().insert(eventsUri, event);


    long eventID = parseLong(url.getLastPathSegment());
    String reminderUriString = "content://com.android.calendar/reminders";

    ContentValues reminderValues = new ContentValues();
    SettingsFragment sf = new SettingsFragment();
    String email = sf.getMainEmail();
    reminderValues.put("event_id", eventID);
    reminderValues.put("minutes", 10); // Default value of the
  // reminderValues.put("method", 1); // Alert Methods: Alert(1) Email(2) SMS(3)
    reminderValues.put("method", 2); // Alert Methods: Alert(1) Email(2) SMS(3)
    Uri reminderUri = getActivity().getApplicationContext().getContentResolver().
            insert(Uri.parse(reminderUriString), reminderValues);