Get Last Updated Time Of Contacts In Android programmatically

I had been trying to write a android program which sync the contacts over the server. I have achieved the syncing part Now I want to maintain the diff part such as only those contacts who have been updated since the last server push may gets pushed in the next server call. I am using the ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP to keep an eye on the last updated timestamp of the contacts. It is also working correctly as it tells me when the contact had been updated But there is a problem, whenever I make any call to any of the contact, it's CONTACT_LAST_UPDATED_TIMESTAMP also gets updated as if the contact has been modified now. I don't want this to happen. I want the exact last updated time of contacts only or by any other means to approach the above situation. Suggestions are welcome. Here's my code

    String phoneNumber = null;

        String _ID = ContactsContract.Contacts._ID;
        String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;

        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String Contact_last  = ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
        ContentResolver contentResolver = appContext.getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);

        ArrayList<Contact> contacts = new ArrayList<>();

        if (cursor!= null && cursor.moveToFirst() && cursor.getCount() > 0) {
            do {

                long activity_at = cursor.getLong(cursor.getColumnIndex(CONTACT_LAST_UPDATED_TIMESTAMP));
                if(activity_at <= prevActivityTime) {

                // Create a contact object
                Contact contact = new Contact();

                // Fetch contact id and contact name
                String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
                String contactName = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));

            } while (cursor.moveToNext());

here preActivityAt is a variable which has timestamp of previous day i.e contacts updated from yesterday till today should get pushed to the server

When you call (or receive a call from) a contact, the values at: ContactsContract.Contact.TIMES_CONTACTED, ContactsContract.Contact.LAST_TIME_CONTACTED change.

So in fact the Contact is updated in the DB, and therefore its LAST_UPDATED timestamp gets updated too.

If you don't care about those two values, then diff between the last contact details you got from this contact, and the new ones, and if none of the fields that are important to you changed, skip that contact.


To avoid storing all the details for all contacts in an app-local DB, you can calculate a hash value (single long integer), that represent the fields currently saved for a contact, if a new hash value for a recently updated contact is different then the previous hash value, you know the contact was modified (on fields that you care about), and that you need to sync that contact.

See this on how to calculate good hash values on Java objects: Java: Getting a unique hash value of an object