Skip to content Skip to sidebar Skip to footer

Android - Get Only Newest Contacts That Were Added

I know how to retrieve the contacts from the user device. But I wonder if there is a way to query the contacts table and get only the newest contacts that were added? What I'm t

Solution 1:

You can achieve using ContentObserver, But you can't get which contact updated, you can get only notify that your native contact is updated.

getContentResolver().registerContentObserver(
                            ContactsContract.Contacts.CONTENT_URI, false, new AddressBookContentObserver());

    /**
     * Content observer for Address book contacts change notification.
     *
     * @author malik
     */
    public class AddressBookContentObserver extends ContentObserver {
        public AddressBookContentObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            if (DBG) {
                ZKLog.d(TAG, "Contacts changes event.");
            }
            // We are waiting for some time, Native contact taking some time ot update, before that
            // if we try to fetch the contact, its not returning the newly added contact
            Timer timer = new Timer();

            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // go through all the contact and check which one is missing or modified or added from your database.
                }
            }, 1000);
        }

        @Override
        public boolean deliverSelfNotifications() {
            return true;
        }
    }

Note :

We can't get which contact is added, modified or deleted


Solution 2:

Starting with API level 18, you can use Contacts.CONTACT_LAST_UPDATED_TIMESTAMP, so it's possible to query for all contacts that had been modified (or created) recently, and compare only those to your last cache of contact ids, and the delta would be the contacts created since the last time your code ran.


Post a Comment for "Android - Get Only Newest Contacts That Were Added"