Listen for new calendar events

和自甴很熟 提交于 2019-12-09 04:24:32

问题


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 BroadcastReceivers 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

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