Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How do you detect which Contact changed?

Is there a way to determine which contact changed?

I know I can register a ContentObserver for the URI but it only triggers when something changes, how am I supposed to know which contact changed and what changed for that contact? Is there a way to find out?

My app involves a desktop client and I would prefer not to send all of the contacts over to the desktop every time it connects. So I would like to keep track of what has changed since the last time the desktop connected.

Thanks in advance!

p.s. I'm using API Level 5+

like image 854
random dude Avatar asked Apr 22 '11 00:04

random dude


1 Answers

No there is no way to get which contact had changed

c&p from my response related with this topic here

I have this code in my Application base class.

private ContentObserver contactObserver = new ContactObserver();

private class ContactObserver extends ContentObserver {

    public ContactObserver() {
        super(null);
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        // Since onChange do not sent which user have been changed, you 
        // have to figure out how to match it with your data.
        // Note: Contact is  one of my classes.
        for (Contact contact : getContacts()) {
            if (!contact.isLinked())
                continue;

            String selection = ContactsContract.Data._ID + " = ?";
            String[] selectionArgs = new String[] { contact.getSystemId() };
            String[] projection = new String[] { ContactsContract.Data.DISPLAY_NAME };
            Cursor cursor = getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, projection,
                    selection, selectionArgs, null);

            if (!cursor.moveToFirst())
                return;

            String name = cursor.getString(0);

            if (contact.getUsername().equalsIgnoreCase(name))
                continue;

            contact.setUserName(name);

        }
    }
}

Regarding about what you can put in projection check here

Hope this helps

like image 103
vsm Avatar answered Oct 19 '22 07:10

vsm