Android-仅获取已添加的最新联系人 [英] Android - Get only newest contacts that were added

查看:84
本文介绍了Android-仅获取已添加的最新联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何从用户设备检索联系人. 但我想知道是否有一种方法可以查询联系人表并仅获取 最新的联系人?

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?

我想做的是:

在我的应用程序中,我从设备读取联系人并将其保存在应用程序的数据库中,因此用户可以选择该应用程序中的收藏夹联系人.下次用户进入联系活动时,我将从数据库中读取数据.

In my application I read the contacts from the device and save them in my application's DB, so the user can select which are the favorites contact in this application. And the next time the user enters to the contact activity, I read the data from the DB.

现在,我想添加一个刷新按钮,因此当用户单击它时,联系人列表将使用新联系人进行更新.因此,而不是再次读取整个联系人表.已添加的联系人或已更改的联系人(在设备中).

Now, I want to add a refresh button, so when the user clicks on it, the contact list will updated with the new contacts.. so instead of reading again the whole contacts table.. I want to retreive only the newest contacts that were added or the ones that were changed (in the device).

有什么办法可以做到这一点?

is there any way to acheive this?

我看到的唯一与此问题相关的字段是"VERSION" 和"DATA_VERSION"

The only fields I see that are somehow related to this question are "VERSION" and "DATA_VERSION"

所以我想我还需要在数据库中保存每个联系人行的版本,并且当用户单击刷新时,我需要对此进行比较,以查看是否有任何更改..但是在这种情况下,我需要再次从设备读取整个联系人...

So I guess I need to save in my DB also the VERSION for each contact row and when to user clickes on refresh I need to compare this in order to see if there was any change.. but in this case I need again to read the entire contacts from the device...

推荐答案

您可以使用ContentObserver实现,但是您无法获取更新的联系人,只能获取有关本机联系人已更新的通知.

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

注意:

我们无法获取添加,修改或删除的联系人

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

这篇关于Android-仅获取已添加的最新联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆