0

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

我正在使用带有 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();

谢谢你。

4

3 回答 3

1

尝试代替 "Canada/Eastern" , timeZone.getID() 像这样:

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

还要检查您何时查找事件,因为某些 Java 日历/日期函数具有 0 索引月份。所以您将 11 月设置为 12 月而不是 11 月。以防万一..

于 2012-12-12T12:19:43.053 回答
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();
}
于 2013-11-15T13:55:58.783 回答
0

尝试这个:

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

奇迹般有效!

于 2014-09-09T12:34:12.327 回答