0

在我的应用程序中,我制作了一个新日历并向其中添加了一些日历事件。

private long addCalendar() {
    Uri.Builder builder;
    ContentValues values;

    if (Build.VERSION.SDK_INT < 14) {
        values = new ContentValues();
        values.put(CalendarConst.Calendars._SYNC_ACCOUNT, calendarname);
        values.put(CalendarConst.Calendars._SYNC_ACCOUNT_TYPE, "LOCAL");
        values.put(CalendarConst.Calendars.NAME, calendarname);
        values.put(CalendarConst.Calendars.DISPLAY_NAME, calendarname);
        values.put(CalendarConst.Calendars.COLOR, calendarcolor);
        values.put(CalendarConst.Calendars.ACCESS_LEVEL, CalendarConst.Calendars.OWNER_ACCESS);
        values.put(CalendarConst.Calendars.TIMEZONE, "Europe/Berlin");
        values.put(CalendarConst.Calendars.OWNER_ACCOUNT, getOwnerAccount());
        values.put(CalendarConst.Calendars.SELECTED, 1); 
        values.put(CalendarConst.Calendars.SYNC_EVENTS, 1);

        Uri contentUri;

        if (Build.VERSION.SDK_INT >= 8) {
            contentUri = Uri
                    .parse("content://com.android.calendar/calendars");
        } else {
            contentUri = Uri.parse("content://calendar/calendars");
        }

        builder = contentUri.buildUpon();
        builder.appendQueryParameter(CalendarConst.Calendars.ACCOUNT_NAME, calendarname);
        builder.appendQueryParameter(CalendarConst.Calendars.ACCOUNT_TYPE, "LOCAL");
        builder.appendQueryParameter(CalendarConst.CALLER_IS_SYNCADAPTER, "true");
    } else {
        values = new ContentValues();
        values.put(Calendars.ACCOUNT_NAME, calendarname);
        values.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        values.put(Calendars.NAME, calendarname);
        values.put(Calendars.CALENDAR_DISPLAY_NAME, calendarname);
        values.put(Calendars.CALENDAR_COLOR, calendarcolor);
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
        values.put(Calendars.CALENDAR_TIME_ZONE, "Europe/Berlin");
        values.put(CalendarContract.Calendars.OWNER_ACCOUNT, getOwnerAccount());
        values.put(CalendarContract.Calendars.VISIBLE, 1);
        values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);

        builder = CalendarContract.Calendars.CONTENT_URI.buildUpon();
        builder.appendQueryParameter(Calendars.ACCOUNT_NAME, calendarname);
        builder.appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        builder.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true");
    }

    Uri uri = contentResolver.insert(builder.build(), values);

    return Long.valueOf(uri.getLastPathSegment());
}

private long addCalendarEvent(CalendarEvent event, long calendarID) {
    ContentValues values = new ContentValues();
    Uri uri;
    long start = Calendar.getInstance().getTimeInMillis();

    if (Build.VERSION.SDK_INT < 14) {
        values.put(CalendarConst.EventsColumns.DTSTART, event.getDtStart());
        values.put(CalendarConst.EventsColumns.DTEND, event.getDtEnd());
        values.put(CalendarConst.EventsColumns.TITLE, event.getTitle());
        values.put(CalendarConst.EventsColumns.EVENT_LOCATION, event.getLocation());
        values.put(CalendarConst.EventsColumns.CALENDAR_ID, calendarID);
        values.put(CalendarConst.EventsColumns.EVENT_TIMEZONE, "Europe/Berlin");
        values.put(CalendarConst.EventsColumns.DESCRIPTION, event.getDescription());
        values.put(CalendarConst.EventsColumns.ALL_DAY, event.isAllDayEvent() ? 1 : 0);

        if (!TextUtils.isEmpty(event.getrRule())) {
            values.put(CalendarConst.EventsColumns.RRULE, event.getrRule());
        }

        uri = contentResolver.insert(CalendarConst.Events.CONTENT_URI, values);
    } else {
        values.put(Events.DTSTART, start);
        values.put(Events.DTEND, event.getDtEnd());
        values.put(Events.TITLE, event.getTitle());
        values.put(Events.EVENT_LOCATION, event.getLocation());
        values.put(Events.CALENDAR_ID, calendarID);
        values.put(Events.EVENT_TIMEZONE, "Europe/Berlin");
        values.put(Events.DESCRIPTION, event.getDescription());
        values.put(Events.ALL_DAY, event.isAllDayEvent() ? 1 : 0);

        if (!TextUtils.isEmpty(event.getrRule())) {
            values.put(Events.RRULE, event.getrRule());
        }

        uri = contentResolver.insert(Events.CONTENT_URI, values);
    }

    return Long.valueOf(uri.getLastPathSegment());
}

但现在我还想收听添加到此日历中的事件。我该怎么做呢?

4

1 回答 1

0

利用内容观察者。您可以在此处或在android 开发人员上找到有关内容观察者的更多信息。您需要创建一个注册观察者的服务。在onChange();函数中,从您上次离开的位置读取数据库(使用内容提供程序)(为此使用共享首选项)。注意所有的变化。

registerContentObserver如果您不希望从传递给调用 onChange() 的 uri 的子目录中进行更改,则在注册观察者时也传递 false ;

于 2013-06-11T19:38:20.057 回答