When a user changes/updates contact details in the contacts app I need to capture the update in that specific contact and get that specific contact ID to make relevant changes to the contact data within my application. So, I use the ContentResolver and ContentObserver classes. And register the ContentObserver with 'ContactsContract.Contacts.CONTENT_URI' in ContentResolver is shown below.

ContentResolver contentResolver = getContentResolver();
contentResolver.registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, new MyContactsObserver(null));

And the MyContactsObserver class is shown below.

public class MyContactsObserver extends ContentObserver {
        private static final String TAG = "MyContactsObserver";
        private Context context;
        public MyContactsObserver(Context context) {
            super(null);
            this.context = context;
        }
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            super.onChange(selfChange, uri);
            Log.d(TAG, "Captured uri: "  + uri.toString());
            Log.d(TAG, "Required uri: "  + ContactsContract.Contacts.CONTENT_URI.toString());
            Log.d(TAG,"Condition :" + Boolean.toString(uri != null && uri.toString().startsWith(ContactsContract.Contacts.CONTENT_URI.toString())))

            if (uri != null && uri.toString().startsWith(ContactsContract.Contacts.CONTENT_URI.toString())) {
                long contactId = ContentUris.parseId(uri);

                Log.d(TAG, "Contact changed: " + contactId);

                Cursor cursor = null;
                try {
                    cursor = context.getContentResolver().query(
                            ContactsContract.Contacts.CONTENT_URI,
                            null,
                            ContactsContract.Contacts._ID + " = ?",
                            new String[]{String.valueOf(contactId)},
                            null);
                    if (cursor != null && cursor.moveToFirst()) {
                        String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Log.d(TAG, "Display name: " + displayName);
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Error querying contact: " + contactId, e);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
            }
        }
    }

When a contact is changed, the above MyContactsObserver is run and onChange method is called. The log output is shown below.

D/MyContactsObserver: Captured uri:  content://com.android.contacts
D/MyContactsObserver: Required uri:  content://com.android.contacts/contacts
D/MyContactsObserver: Condition   :  false
            

The ContentObserver capture the contact change flag, but didn't capture the ContactsContract.Contacts.CONTENT_URI

0

There are 0 best solutions below