我插入了这样一个日历合约条目:
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
values.put(CalendarContract.Calendars.NAME, name);
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, getDisplayName(account));
values.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xffff0000);
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, getMailAddressOf(account));
values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getTimeZone("GMT").getDisplayName());
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
Uri.Builder builder = CalendarContract.Calendars.CONTENT_URI.buildUpon();
builder.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
builder.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
builder.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true");
Uri result = getContext().getContentResolver().insert(builder.build(), values);
这很好用,我可以输入日历条目等。但我现在需要将其设置为只读,这样用户就无法编辑日历条目。
我认为,当我将 CALENDAR_ACCESS_LEVEL
设置为 CAL_ACCESS_READ
时,应该足以让日历只读。
知道如何实现吗?顺便说一句,我正在使用 Android O 的 Pixel 上进行测试。
最佳答案
不要让用户成为组织者。添加 Activity 时,默认组织者是用户。要更改它,请在添加事件时将此行添加到 ContentValues,像这样分配组织者电子邮件地址
values.put(CalendarContract.Events.ORGANIZER,"an_organizer_name@gmail.com";
由于用户不是组织者,用户仍然可以删除 Activity ,但不能编辑 Activity 内容,如时间、标题、描述等
那花了一些时间。最后我发现我必须更改 CalendarContract.Events.ORGANIZER
值的邮件地址以防止我可以编辑日历条目。就像对其他人我如何解决这个问题的提示一样,我尝试了很多愚蠢的事情,但后来我有想法检查它是如何在 AOSP 中完成的。所以我只是搜索“calendar aosp 代码” 并在 GitHub 上找到了该代码。在 repo 中使用代码搜索后,我找到了方法 canModifyEvent(...)
.现在它忘记了:
public static boolean canModifyEvent(CalendarEventModel model) {
return canModifyCalendar(model)
&& (model.mIsOrganizer || model.mGuestsCanModify);
}
确保客人不能编辑 Activity ,也不能是组织者。
编码愉快!
关于android - 将日历条目设置为只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45247314/