2

In my Android app, an Activity performs a query to get the contacts on the phone and stores them into a sqlite database. For now, this is performed only once during the first launch of the Activity because that operation takes about two seconds to execute (which is actually a lot of time)

However that workaround comes with an issue: if the phone user adds some contact my app will never be able to get them. Then, I was wondering if it was possible for my app to ´listen ´ for that event and add the contact in the database when it occurs.

Is such a thing possible?

Kritias
  • 187
  • 1
  • 12

1 Answers1

0

You can do it by using ContentObserver.

You have to simply

  • Implement a subclass of ContentObserver

  • you have to register your ContentObserver to listen the change.

For more detail how to do it you can follow this article Use Android’s ContentObserver in Your Code to Listen to Data Changes

BBdev
  • 4,898
  • 2
  • 31
  • 45
  • Thank you that is exactly what I need. I read the article you provided but can you just tell me if the Observer will notice a change even if the application isn't running during that change? – Kritias May 20 '15 at 12:52
  • No the life cycle of content observer ends when your application is not running. So it cannot listen to change if your application is closed. – BBdev May 21 '15 at 04:29
  • That´s quite unfortunate then, an app is not supposed to be opened forever. Is there any workaround or other solution for this? – Kritias May 21 '15 at 09:21