【问题标题】:Make calendar entry readonly将日历条目设为只读
【发布时间】:2017-12-16 05:52:43
【问题描述】:

我插入了一个日历合同条目,如下所示:

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 上进行测试。

【问题讨论】:

    标签: android calendar android-calendar


    【解决方案1】:

    不要让用户成为组织者。添加事件时,默认组织者将是用户。要更改它,请在添加事件时将此行添加到 ContentValues,分配一个像这样的组织者电子邮件地址

    values.put(CalendarContract.Events.ORGANIZER,"an_organizer_name@gmail.com";
    

    由于用户不是组织者,用户仍然可以删除活动,但不能编辑活动内容,如时间、标题、描述等。

    这需要一些时间。最后我发现我必须更改CalendarContract.Events.ORGANIZER 值的邮件地址,以防止我可以编辑日历条目。就像向其他人暗示我如何解决这个问题一样,我尝试了很多愚蠢的事情,但后来我想到了检查它是如何在 AOSP 中完成的。于是我就随便搜索了“calendar aosp code”,发现在GitHub上镜像的代码。在 repo 中使用代码搜索后,我找到了方法canModifyEvent(...)。现在它变得健忘:

    public static boolean canModifyEvent(CalendarEventModel model) {
        return canModifyCalendar(model)
                && (model.mIsOrganizer || model.mGuestsCanModify);
    }
    

    确保活动不能由客人编辑,并且不是组织者。

    编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 2011-09-04
      • 1970-01-01
      • 2017-03-08
      相关资源
      最近更新 更多