Android Calendar Intent event end always one hour

2019-08-20 03:31发布

I've looked into this problem a lot and I can't seem to find anyone having the same difficulties. The start time is working fine but the end time is always set to being exactly one hour after the initial start time of the event.

            Calendar calStart = Calendar.getInstance();
                  calStart.set(2014, 02, 04, 13, 20, 0);

    Calendar calEnd = Calendar.getInstance();
                  calEnd.set(2014, 02, 04, 15, 30, 0);

    Intent intent = new Intent(Intent.ACTION_INSERT)
    .setData(Events.CONTENT_URI)
     .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calStart.getTimeInMillis())     
    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, calEnd.getTimeInMillis())
    .putExtra(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID())
    .putExtra(Events.TITLE, "TEST")
    .putExtra(Events.EVENT_LOCATION, "Location")
    .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);

     startActivity(intent);

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-20 03:52

I fixed it my using a different implementation.

 public void setAlarm(Date examMonth, int day, int hourOfDay, int minute,
        double length, String examName, String venue) {


    Calendar calSet = Calendar.getInstance();
    calSet.setTime(examMonth);
    int month = calSet.get(Calendar.MONTH);
    int year = Calendar.getInstance().get(Calendar.YEAR);
    calSet.set(year, month, day, hourOfDay, minute, 0); 



     // look for calendar
    Cursor cursor = getContentResolver()
            .query(Uri.parse("content://com.android.calendar/calendars"),
                    new String[] { "_id", "calendar_displayName" },"visible='1'"
                    , null, null);
    cursor.moveToFirst();
    String[] CalNames = new String[cursor.getCount()];
    int[] CalIds = new int[cursor.getCount()];
    for (int i = 0; i < CalNames.length; i++) {
        CalIds[i] = cursor.getInt(0);
        CalNames[i] = cursor.getString(1);
        cursor.moveToNext();
    }

    cursor.close();


    // put calendar event
    ContentValues event = new ContentValues();
    event.put("calendar_id", CalIds[0]);
    event.put("title", examName);
    //event.put("description", description);
    event.put("eventLocation", venue);
    event.put(Events.ALL_DAY, 0);
    event.put("dtstart",  calSet.getTimeInMillis());
    event.put("dtend", (float)(length*3600000)+calSet.getTimeInMillis());
    event.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
    event.put(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);
    event.put("hasAlarm", 1);

    Uri eventsUri = Uri.parse("content://com.android.calendar/events");
    Uri newEvent = getContentResolver().insert(eventsUri, event);

    // put alarm reminder for an event, 2 hours prior
    long eventID = Long.parseLong(newEvent.getLastPathSegment());

    ContentValues cv_alarm = new ContentValues();
    cv_alarm.put("event_id", eventID);
    cv_alarm.put("method", 1);
    cv_alarm.put("minutes", 120);
    this.getContentResolver()
            .insert(Uri.parse("content://com.android.calendar/reminders"),
                    cv_alarm);
}
查看更多
登录 后发表回答