听取android日历的变化。 (同步/删除/插入等..)

时间:2013-11-16 12:46:39

标签: android android-contentprovider android-calendar

我知道我必须使用Content Provider来获取所有更改,但我也意识到开始API14日历已准备就绪Content Provider我可以用来收听而不是“建立”我自己的自定义 我有什么地方可以看到这样的例子吗? 有人可以发布这个听众的核心吗?

由于

1 个答案:

答案 0 :(得分:23)

首先,您需要将此类接收器添加到Manifest:

    <receiver android:name="your.package.name.CatchChangesReceiver"
        android:priority="1000" >
        <intent-filter>
            <action android:name="android.intent.action.PROVIDER_CHANGED" />
            <data android:scheme="content" />
            <data android:host="com.android.calendar" />
        </intent-filter>
    </receiver>

然后创建一个扩展广播接收器的简单类:

public class CatchChangesReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    // add processing here with some query to content provider
        // in my project I use this selection for getting events:
         final String SELECTION = CalendarContract.Events.CALENDAR_ID + "="
            + calendarId + " AND " + "("
            + CalendarContract.Events.DIRTY + "=" + 1 + " OR "
            + CalendarContract.Events.DELETED + "=" + 1 + ")" + " AND "
            + CalendarContract.Events.DTEND + " > "
            + Calendar.getInstance().getTimeInMillis();
    }
}