Listen to android calendar changes. (Sync/Delete/Insert etc..)

与世无争的帅哥 提交于 2019-11-29 02:52:27

问题


I've understand I have to use Content Provider to get all changes, but I also realized starting API14 there is a ready Content Provider for the calendar which I can use to listen to instead of "building" my own custom one.
Is there anywhere I can see an example of this ?
Can someone please post the core of this listener ?

Thanks


回答1:


First you need to add this type of receiver to the 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>

Then create a simple class which extends broadcast 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();
    }
}


来源:https://stackoverflow.com/questions/20018619/listen-to-android-calendar-changes-sync-delete-insert-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!