问题
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?
回答1:
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 BroadcastReceiver
s must complete within 10 seconds as per the onReceive() documentation).
回答2:
<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
来源:https://stackoverflow.com/questions/21168766/listen-for-new-calendar-events