代码未将事件插入日历

时间:2012-09-15 20:47:10

标签: android events insert calendar

有谁知道为什么此代码没有将事件插入日历?我没有错误,它只是没有插入事件?

我正在使用带有ICS更新的galaxy s2,不确定这是否相关,但只是想知道它是否与它不是Google日历应用程序有关。

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

谢谢。

3 个答案:

答案 0 :(得分:1)

尝试代替“加拿大/东方”,timeZone.getID(),如下所示:

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

同时检查你何时查找事件,因为一些java日历/日期函数有0索引月。所以你设置的月份是12月12月而不是11月。只是以防万一..

答案 1 :(得分:0)

不确定它是否已解决。尝试以下代码,它正在4.0上工作 注意月份是0基于所以2103,12,解决到2014年1月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();
}

答案 2 :(得分:0)

试试这个:

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

就像一个魅力!

相关问题