2012-09-15 24 views
0

有沒有人看到爲什麼這段代碼沒有將事件插入日曆?我沒有錯誤,它只是不插入事件?代碼沒有將事件插入日曆

我正在使用星系s2與ICS更新,不確定這是否相關,但只是想知道它是否與它不是谷歌日曆應用程序有關。

public void addEvent(Context ctx, String title, Calendar start, Calendar end) { 
    Log.d(TAG, "AddUsingContentProvider.addEvent()"); 

TextView calendarList = 
    (TextView) ((Activity) ctx).findViewById(R.id.calendarList); 

ContentResolver contentResolver = ctx.getContentResolver(); 

ContentValues calEvent = new ContentValues(); 
calEvent.put(CalendarContract.Events.CALENDAR_ID, 1); // XXX pick) 
calEvent.put(CalendarContract.Events.TITLE, title); 
calEvent.put(CalendarContract.Events.DTSTART, start.getTimeInMillis()); 
calEvent.put(CalendarContract.Events.DTEND, end.getTimeInMillis()); 
calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "Canada/Eastern"); 
Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, calEvent); 

// The returned Uri contains the content-retriever URI for 
// the newly-inserted event, including its id 
int id = Integer.parseInt(uri.getLastPathSegment()); 
Toast.makeText(ctx, "Created Calendar Event " + id, 
    Toast.LENGTH_SHORT).show(); 

謝謝。

回答

1

嘗試,而不是「加拿大/東方」,timeZone.getID()這樣的:

TimeZone timeZone = TimeZone.getDefault(); 
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); 

當您要查找的事件同時檢查,因爲一些Java日曆/日期函數有0索引一個月。所以你設置的月份是11月份,十二月份不是十一月。只是如果..

0

不知道是否解決了。嘗試下面的代碼,它正在開發4.0 當心月是0基於這樣的2103,12,決心到2014年,月,20 {

Calendar beginTime = Calendar.getInstance();//it puts in 2014, Jan 20 
    beginTime.set(2013, 12, 20, 7, 30); 
    Calendar endTime = Calendar.getInstance(); 
    endTime.set(2013, 12, 20, 8, 30); 

    ContentValues calEvent = new ContentValues(); 
    calEvent.put(CalendarContract.Events.CALENDAR_ID, 1); // XXX pick) 
    calEvent.put(CalendarContract.Events.TITLE, "title is game time"); 
    calEvent.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis()); 
    calEvent.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis()); 
    calEvent.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID()); 
    Uri uri = this.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, calEvent); 

    // The returned Uri contains the content-retriever URI for 
    // the newly-inserted event, including its id 
    int id = Integer.parseInt(uri.getLastPathSegment()); 
    Toast.makeText(this, "Created Calendar Event " + id, 
     Toast.LENGTH_SHORT).show(); 
} 
0

試試這個:

calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, CalendarContract.Calendars.CALENDAR_TIME_ZONE); 

奇蹟般有效!