Android日历Intent事件始终在启动后一小时结束

时间:2014-02-08 22:50:47

标签: android android-intent android-calendar

我经常调查这个问题而且我似乎找不到任何有同样困难的人。开始时间正常,但结束时间始终设置为事件初始开始时间后一小时。

            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 个答案:

答案 0 :(得分:0)

我使用不同的实现修复了它。

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