有没有办法确定哪个联系人已更改?

我知道我可以为URI注册一个ContentObserver,但是它仅在发生某些更改时才触发,我应该如何知道哪个联系人更改了以及该联系人更改了什么?有没有办法找出来?

我的应用程序包含桌面客户端,我不希望每次连接时都将所有联系人都发送到桌面。因此,我想跟踪自上次连接桌面以来发生的变化。

提前致谢!

ps。我正在使用API​​ Level 5+

最佳答案

没有,无法获取更改的联系人

我的回复中与该主题相关的c&p here

我的应用程序基类中有此代码。

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);

        }
    }
}

关于可以放入投影检查here的内容

希望这可以帮助

08-25 23:16
查看更多