Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen for new calendar events

Mainly what I need is to react depending on the events in the default android calendar (Google).

It's fairly easy to query the provider and get the events at any given time but my data has to be consistent all the time. What I want is to avoid querying the provider each x min to check for new events.

I couldn't find any system broadcast for when the app stores the new event (only when the event gets triggered). Just to be clear, I don't want to create, delete, etc events from my app, I just want to monitorize them, I need the moment of the creation of a new event so I can requery the provider.

I know Google has a (very poorly documented) Calendar API, any experience with it? Does it suit my needs?

like image 685
MariusBudin Avatar asked Jan 16 '14 17:01

MariusBudin


2 Answers

As explained in this answer to a similar question, you can use a BroadcastReceiver to receive change events from the default Android calendar by creating a BroadcastReciver with the following intent filter:

<receiver
   android:name=".NativeEventChangeReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PROVIDER_CHANGED"/>
            <data android:scheme="content"/>
            <data android:host="com.android.calendar"/>
        </intent-filter>
</receiver>

Note that no information is passed along with this receiver - you'll need to query the calendar API upon receipt of that to find what has changed. If you are doing an operation that may take a long time, you may want to consider using a WakefulBroadcastReceiver and an IntentService to do the longer running process (as BroadcastReceivers must complete within 10 seconds as per the onReceive() documentation).

like image 192
ianhanniballake Avatar answered Nov 06 '22 20:11

ianhanniballake


     <receiver android:name=".MyBroadcastReceiver"  android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.EVENT_REMINDER" />
            <data android:scheme="content"/>
            <data android:host="com.android.calendar"/>
        </intent-filter>
    </receiver>

this will trigger your onReceive(). Set an EVENT in your calendar with a 10 min notification before the event. The trigger will take place ONLY at the notification time

like image 1
Dan Alboteanu Avatar answered Nov 06 '22 19:11

Dan Alboteanu