Question

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);
Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top