Android 如何检测哪个联系人发生了变化?

发布于 2024-11-02 12:37:54 字数 231 浏览 1 评论 0原文

有没有办法确定哪个联系人发生了变化?

我知道我可以为 URI 注册一个 ContentObserver,但它仅在某些内容发生变化时才会触发,我该如何知道哪个联系人发生了变化以及该联系人发生了什么变化?有办法找出来吗?

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

提前致谢!

ps 我使用的是 API 级别 5+

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+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2024-11-09 12:37:54

不,无法

从我与此主题相关的回复中获取哪个联系人更改了 c&p 此处

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

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

        }
    }
}

关于您可以在投影中放入的内容,请检查此处

希望如此帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文