gpt4 book ai didi

Android 日历事件不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:14 24 4
gpt4 key购买 nike

您好,我有以下代码可以在我的日历上添加 Activity :

public String addCalendarEntry(CalendarDTO calendar) {

ContentValues event = new ContentValues();
ContentResolver cr = getContentResolver();

long startMillis = calendar.getStartDate().getTimeInMillis();
long endMillis = calendar.getEndDate().getTimeInMillis();
String timeZone = TimeZone.getDefault().getID();

event.put(CalendarContract.Events.CALENDAR_ID, calendar.getId());
event.put(CalendarContract.Events.TITLE, calendar.getTitle());
event.put(CalendarContract.Events.DESCRIPTION, calendar.getDescription());
event.put(CalendarContract.Events.EVENT_LOCATION, calendar.getLocation());
event.put(CalendarContract.Events.DTSTART, startMillis);
event.put(CalendarContract.Events.DTEND, endMillis);
event.put(CalendarContract.Events.ALL_DAY, Boolean.getBoolean(String.valueOf(calendar.isAllDay()))); // 0 for false, 1 for true
event.put(CalendarContract.Events.HAS_ALARM, 1); // 0 for false, 1 for true
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);

Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, event);

return uri.getLastPathSegment();
}

这段代码直到前几天都运行良好,突然间我的日历中不再添加任何事件。它也没有错误,所以我在这里不知所措。

CalendarDTO 本身只是一个数据传输类:

public class CalendarDTO {
int id;
String title;
String description;
String location;
Calendar startDate;
Calendar endDate;
boolean isAllDay;

// getters and setters removed for brevity
}

非常感谢任何线索。

最佳答案

您是否使用 calendar.getId() 检查是否存在日历?

来自文档:http://developer.android.com/reference/android/provider/CalendarContract.Events.html

Writing to Events There are further restrictions on all Updates and Inserts in the Events table:

If allDay is set to 1 eventTimezone must be TIMEZONE_UTC and the time must correspond to a midnight boundary.

尝试这样的事情:

event.put(CalendarContract.Events.CALENDAR_ID, calendar.getId());
event.put(CalendarContract.Events.TITLE, calendar.getTitle());
event.put(CalendarContract.Events.DESCRIPTION, calendar.getDescription());
event.put(CalendarContract.Events.EVENT_LOCATION, calendar.getLocation());
if(calendar.isAllDay){
Calendar start=calendar.getStartDate();
start.set(Calendar.HOUR_OF_DAY, 0);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
Calendar end=start;
end.add(Calendar.DAY_OF_MONTH,1);
event.put(CalendarContract.Events.DTSTART, start.getTimeInMillis());
event.put(CalendarContract.Events.DTEND, end.getTimeInMillis());
event.put(CalendarContract.Events.EVENT_TIMEZONE, Time.TIMEZONE_UTC);
}else{
event.put(CalendarContract.Events.DTSTART, startMillis);
event.put(CalendarContract.Events.DTEND, endMillis);
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
}
event.put(CalendarContract.Events.HAS_ALARM, 1);
event.put(CalendarContract.Events.ALL_DAY, calendar.isAllDay()?1:0);

关于Android 日历事件不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31146936/

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