如何插入/更新/删除通讯录听联系 [英] How to listen contact inserted/updated/deleted in address book

查看:193
本文介绍了如何插入/更新/删除通讯录听联系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多与之相关的问题,但没有人帮助我获得解决方案。

There are lots of questions related to it but none of them help me to get the solution.

我想从同步设备的所有接触到远程服务器,并能够很容易地做到这一点,但是当有像更新接触的更改/删除/插入(新联系人)无法找到解决方案。

I am trying to sync all contacts from device to remote server and able to do it easily but when there is a change in contact like update/delete/insert(new contact) unable to find the solution.

使用 ContentObserver ,但的onChange()获取调用多次尝试。这是很难找到的联系人数据发生变化。

Tried using ContentObserver but onChange() is getting called multiple times. It's difficult to find the contact changes data.

    public class ContactService extends Service {

    private int mContactCount;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContactCount = getContactCount();

        Log.d("Contact Service", mContactCount + "");

        this.getContentResolver().registerContentObserver(
                ContactsContract.Contacts.CONTENT_URI, true, mObserver);
    }

    private int getContactCount() {
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, null, null, null,
                    null);
            if (cursor != null) {
                return cursor.getCount();
            } else {
                return 0;
            }
        } catch (Exception ignore) {
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return 0;
    }

    private ContentObserver mObserver = new ContentObserver(new Handler()) {

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

            final int currentCount = getContactCount();
            if (currentCount < mContactCount) {
                // CONTACT DELETED.

                Log.d("Contact Service", currentCount + "");

            } else if (currentCount == mContactCount) {
                // CONTACT UPDATED.
            og.d("Contact Service", currentCount+"");

            } else {
                // NEW CONTACT.
                Log.d("Contact Service", currentCount + "");

            }
            mContactCount = currentCount;
        }

    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getContentResolver().unregisterContentObserver(mObserver);
    }
}

的onChange()获取调用一次以上时,有一个更新/插入到地址簿中。

But onChange() getting called more than once when there is a update/insert into address book.

谁能给我提供更好的解决方案呢?

can anyone provide me better solution to it?

这将是非常美联社preciated。

It would be highly appreciated.

感谢

推荐答案

有关的onChange 的事情是,它被称为既为删除/添加/更新,所以你可以牛逼算了算,因为人们可能已被删除,一个补充,那么你有改变的联络簿,但同样数量的联系人的数量。但是看着版本的 <一个href="http://developer.android.com/reference/android/provider/ContactsContract.SyncColumns.html#VERSION">column你应该能够评估其接触被更新时(你获得的通讯录中的一个完整副本后话)。简单地检查版本大于一个你已经(为当前联系人)。

The thing about onChange is that it gets called both for delete/add/update so you can't just count the number of contacts since one might have been deleted and one added then you have a changed contact book, but same count. However looking at the version column you should be able to assess which contact is updated or not (after you've obtained one complete copy of the contact book already). Simply check if the version is greater than the one you have already (for the current contact).

这篇关于如何插入/更新/删除通讯录听联系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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