聆听日历中的更改

时间:2013-06-11 15:04:29

标签: android android-contentprovider android-calendar

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

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());
}

但是现在我也想听一下添加到这个日历的事件。 我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

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

如果您不希望从传递给uri的子目录更改为调用onChange();

,则在使用registerContentObserver注册观察者时传递false