gpt4 book ai didi

android - 无法在 Google 日历中插入多个事件

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:12 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 CalendarContract API 来管理 Google 日历上的日历和事件。将多个事件添加到单个日历时,我遇到了一些问题。一般来说,这种方法的问题是每当我向谷歌日历添加新事件时,旧事件就会被新事件取代。这是我的方法的代码:

    public int addEvent(Context context, String color, int calendarId, String name, String location, String description, Date dateBegin, Date dateEnd) {
long startMillisEpoch;
long endMillisEpoch;

Calendar beginTime = Calendar.getInstance();
beginTime.setTime(dateBegin);
startMillisEpoch = beginTime.getTimeInMillis();

Calendar endTime = Calendar.getInstance();
endTime.setTime(dateEnd);
endMillisEpoch = endTime.getTimeInMillis();

int eventColor;
try {
eventColor = Color.parseColor(color);
} catch (Exception e) {
// Get a random color
Random random = new Random();
eventColor = MyApp.RANDOM_COLORS[random.nextInt(MyApp.RANDOM_COLORS.length)];
}
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
values.put(CalendarContract.Events.DTSTART, startMillisEpoch);
values.put(CalendarContract.Events.DTEND, endMillisEpoch);
values.put(CalendarContract.Events.TITLE, name);
values.put(CalendarContract.Events.EVENT_LOCATION, location);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.EVENT_COLOR, eventColor);
// NOTE: Every event MUST have a timezone. Otherwise,
// the application will throw an IllegalArgumentException
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getDisplayName());
// Put default values
values.put(CalendarContract.Events.ALL_DAY, NON_ALL_DAY);
values.put(CalendarContract.Events.GUESTS_CAN_INVITE_OTHERS, 1);
Uri calUri = context.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, values);
if (calUri != null) {
try {
return Integer.parseInt(calUri.getLastPathSegment());
} catch (Exception e) {
return -1;
}
}
return -1;
}

有解决此类问题的想法吗?亲切的问候。

最佳答案

根据API我猜你错过了 .execute()

Uri calUri = context.getContentResolver()
.insert(CalendarContract.Events.CONTENT_URI, values)
.execute();

还有 check this question

关于android - 无法在 Google 日历中插入多个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30236111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com