Shane
Shane

Reputation: 2375

Lifecycle of a ContentObserver?

What is the life-cycle of a ContentObserver? Once registered, does it persist after the app has been closed?

If the ContentObserver is unregistered after the app is closed, is there a way around this? For my specific case, I want to monitor the phone contacts and run some code once a contact has been modified. This needs to happen whenever the contact is changed, not just when my app is in focus.

Upvotes: 3

Views: 1228

Answers (1)

Aditya
Aditya

Reputation: 5619

The observer needs to be in a service. Register the observer in the onCreate(). You will use contentProvider in the onChange of the contentObserver. You will need to maintain time when you last read the database using shared preferences. Note the changes of entries after the time stored in shared preferences. Now update the time of shared preferences to current time. Also unregister the content observer in onDestroy().

Upvotes: 1

Related Questions