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

1

12

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

Kernan answered 16/11, 2013 at 12:46 Comment(0)
M
25

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();
    }
}
Mirthamirthful answered 16/11, 2013 at 13:10 Comment(5)
This is otherwise impossible to get from the documentation, so thanks alot for the insight.Heartsease
Can I get full example?Finespun
@yurezcv: I am new to android development , How can I register the broadcastReceiver ServiceColtish
This has always worked for me when using Samsung devices. Now I bought an Asus Zenfone Max and installed my application on it. All of the features work but the calendar events listener. Why? My Samsung devices had S Planner installed as Calendar app but the Asus one has the Google Calendar. Should that make a difference?Orme
Doesn't always work when deleting recurring event instances (not the entire event). Cannot identify the events that have changed.Matchlock

© 2022 - 2024 — McMap. All rights reserved.