2014-02-08 57 views
0

我已經仔細研究過這個問題,我似乎無法找到任何人遇到同樣的困難。開始時間工作正常,但結束時間始終設置爲事件初始開始時間後的一個小時。Android日曆意圖事件結束總是在開始後一小時

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

試試這個: intent.putExtra(Events.DTSTART,UTC millis自epoch以來); intent.putExtra(Events.DTEND,UTC millis自epoch以來); – KratosBala

回答

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); 
} 
相關問題