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

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

问题描述

有很多与此相关的问题,但没有一个能帮助我找到解决方案.

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.

尝试使用 ContentObserveronChange() 被多次调用.很难找到联系人变动数据.

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?

不胜感激.

谢谢

推荐答案

关于 onChange 的事情是它在删除/添加/更新时都会被调用,所以你不能只计算数量联系人,因为一个可能已被删除并添加了一个,那么您的通讯录已更改,但数量相同.但是查看版本 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天全站免登陆