6

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?

MariusBudin
  • 1,277
  • 1
  • 10
  • 21

2 Answers2

11

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).

Community
  • 1
  • 1
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    Didn't know about the `PROVIDER_CHANGED` broadcast. +1 for your great answer! . Will accept it when I implement it this weekend. Thank you! – MariusBudin Jan 16 '14 at 18:02
  • How do I make this work for Android Oreo and above? I have tried this programmatically but I am not seeing any receiver events on calendar changes. Have been stuck on this for 3 days now. – user2511882 Apr 10 '20 at 14:40
1
     <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

Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40