6

Isn't there a timestamp for each contact?

I need to be notified by a change (add, delele or change) to a contact. I've implemented a ContentObserver, but trough this method I'm only able to be notified if a change occurs on a contact, but I don't know which contact has been modified! Any suggestions?

Tobbe
  • 1,825
  • 3
  • 21
  • 37
Giancarlo
  • 61
  • 1
  • 2
  • 6
  • @Giancario I am facing the same type of situation.Can you tell me how you solved this problem. – rakesh Jan 04 '14 at 16:00

2 Answers2

0

I had to do something similar. What I did was to sync on basis of timestamp. I keep track of the latest time at which a contact was updated using the field :

ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP

And then I proceed with my sync operation and every time updating my member variable to latest sync time. When sync is complete, this has the last contact sync time.

When the change observers register something, I query content provider for all the rows impacted after this time. And then I iterate over the cursor and process each row.

ShReYaNsH
  • 302
  • 2
  • 10
  • But what if the actual contact id changed with the update? How do you recognize it as being updated? Or do you just delete and add? – epic Jan 21 '20 at 21:57
0

check ContactsContract.RawContacts Column

int DIRTY 

read/write Flag indicating that VERSION has changed, and this row needs to be synchronized by its owning account. The value is set to "1" automatically whenever the raw contact changes, unless the URI has the CALLER_IS_SYNCADAPTER query parameter specified. The sync adapter should always supply this query parameter to prevent unnecessary synchronization: user changes some data on the server, the sync adapter updates the contact on the phone (without the CALLER_IS_SYNCADAPTER flag) flag, which sets the DIRTY flag, which triggers a sync to bring the changes to the server.

This column is used by sync adapters to start sync for a contact when contact is modified. Check if you get anything from this.

om252345
  • 2,395
  • 4
  • 29
  • 31
  • I know this field, but I need to copy all the contacts in a saparete DB on the Android device.The problem is that if I change a contact the Dirty filed is setted to 1, but if I sync this contact with the relative account before I open my App, the Dirty field is setted to 0. Do you think I can use a Service to capture all the changes and store all the Ids whose Dirty flags have been modified in a separate Array (for example) ??? – Giancarlo Dec 07 '11 at 09:43
  • I have not used dirty field. But it should be modified by a update query manually. Do not use a service to monitor changes, use Singleton or Application class instead to check any changes in contacts. – om252345 Dec 07 '11 at 09:46
  • Singleton runs even if my app is not running??? Can you suggest me an example of how I can use singleton to chech any changes in contacts ??? Do I need to put inside the Singleton the ContentObserver ? – Giancarlo Dec 07 '11 at 11:58
  • 1
    You can extend application class and put your observers inside that, that will be active even your activities from app is not active as long as your app is installed. Check this http://groups.google.com/group/android-developers/browse_thread/thread/f72178a1c52aa51?pli=1 – om252345 Dec 07 '11 at 12:14
  • @om252345 will you please look at my problem [issue](http://stackoverflow.com/questions/38012627/dirty-flag-not-getting-set-to-1-on-contact-update) – Naroju Jun 24 '16 at 14:21