2

I am using the following code:

String selection = "((dtstart >= " + now
        + ") AND (dtend <= " + endTime.getTimeInMillis() + "))";
Cursor cursor = context.getContentResolver()
        .query(Uri.*parse*("content://com.android.calendar/events"),
                *PROJECTION*, selection,null, null);

And I notice that once I add/remove an Event to Google Calendar app on the device and run the above code immediately, occasionally I do not get the latest dataset from the Cursor. I have to manually refresh the Google Calendar app to get the latest data.

Am I missing something? Can I use some other APIs?

Thanks!

user2511882
  • 9,022
  • 10
  • 51
  • 59
  • "Am I missing something?" -- the behavior of the Google Calendar app is up to its developers. There is no requirement for them to ever update that provider, let alone do so in some specific timeframe. For that matter, there is no requirement for them to update their own server in a specific timeframe. If they elect to buffer and defer updating their Web service, that's their call. – CommonsWare Apr 05 '20 at 21:10
  • 1
    @CommonsWare Does the documentation for ``CalendarProvider`` state that the data may not be up-to-date? – user2511882 Apr 05 '20 at 22:03
  • AFAIK, the documentation for `CalendarProvider` makes no claims one way or another regarding the fresheness its contents. There is no requirement for any calendar app to even *use* `CalendarProvider`. They can use their own database, or server, or whatever instead of, or in addition to, `CalendarProvider`. Now, in an ideal world, Google Calendar would quickly update `CalendarProvider`. I think it is safe to say that this is not an ideal world. :-( My point is that you are making assumptions about the behavior of particular apps, and those are really more hopes and dreams. – CommonsWare Apr 05 '20 at 22:11
  • 1
    @CommonsWare So what would be a recommended approach in that case. I would like to avoid to use Google sign-in thing if possible. – user2511882 Apr 05 '20 at 22:13
  • @CommonsWare I guess a better question would be, what is the difference between ``Calendar`` API vs ``CalendarProvider``. – user2511882 Apr 05 '20 at 22:24
  • "So what would be a recommended approach in that case" -- I would try to make few assumptions about the freshness of the data in `CalendarProvider`. Beyond that, I don't know what to tell you. – CommonsWare Apr 05 '20 at 22:42

2 Answers2

1

You are right. From some reasons, whether it's a bug or a "feature" - Google calendar provider in last 4-5 months DOES NOT expose changes via CalendarContract until after they have synced with their server.

Before it was not like this, all changes were instantly available for 3rd party apps via CalendarContract - now only after syncing. It is very annoying...You never know if you have fresh data unless you manually perform synchronizing in Google Calendar app (or you can wait until auto-syncing triggers - which can be within seconds, minutes or hours, it is not in your hands).

Such a stupid, stupid change. Classic google. They screwed up so many things already, this is just another one. They just complicate life for devs.

qkx
  • 2,383
  • 5
  • 28
  • 50
0

To get latest data after adding event to calendar you can listen calendar for event changes

ygngy
  • 3,630
  • 2
  • 18
  • 29
  • 1
    I am seeing that the listener is not called consistently either. – user2511882 Apr 10 '20 at 13:52
  • @user2511882 after Android 8 this listener should not be registered in manifest it should be registered dynamically in your activity or fragment or foreground service: https://developer.android.com/about/versions/oreo/background#broadcasts – ygngy Apr 10 '20 at 16:47
  • 1
    I knew that. Still not consistent. – user2511882 Apr 10 '20 at 19:05