首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何监听通讯录中插入/更新/删除的联系人

如何监听通讯录中插入/更新/删除的联系人
EN

Stack Overflow用户
提问于 2014-02-09 03:13:41
回答 2查看 10.6K关注 0票数 18

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

我正在尝试同步所有联系人从设备到远程服务器,并能够轻松地做到这一点,但当有一个像更新/删除/插入(新联系人)联系方式的变化,无法找到解决方案。

已尝试使用ContentObserver,但多次调用onChange()。很难找到联系人变更数据。

代码语言:javascript
复制
    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()会被多次调用。

有人能为我提供更好的解决方案吗?

我将不胜感激。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-06-25 02:52:39

根据Magnus的回答,使用此列检索版本代码

ContactsContract.RawContacts.VERSION

票数 1
EN

Stack Overflow用户

发布于 2021-01-29 10:41:02

这对我来说工作得很好。

1-注册服务

代码语言:javascript
复制
this.getApplicationContext()
                .getContentResolver()
                .registerContentObserver(
                        ContactsContract.Contacts.CONTENT_URI, true,
                        new ContactsListner(new Handler(),this,getContactCount()));

2- getContactCount()的定义

代码语言:javascript
复制
private int getContactCount() {
        Cursor cursor = null;
        try {
            cursor = activity.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;
    }

3- Contact listner类

代码语言:javascript
复制
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.ContactsContract;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class ContactsListner extends ContentObserver {
    /**
     * Creates a content observer.
     *
     * @param handler The handler to run {@link #onChange} on, or null if none.
     *
     *
     */

    Activity activity;
    int contactsCount;

    public ContactsListner(Handler handler, Activity activity, int contactsCount) {
        super(handler);
        this.activity = activity;
        this.contactsCount = contactsCount;
    }




    @Override
    public void onChange(boolean selfChange, @Nullable Uri uri) {
        super.onChange(selfChange, uri);



            int currentContacts = getContactCount();
            Toast.makeText(activity, "Current num is "+currentContacts+"     and recent : "+contactsCount, Toast.LENGTH_SHORT).show();

            if (currentContacts<contactsCount){
                Toast.makeText(activity, "Deleted", Toast.LENGTH_SHORT).show();
            }else if (currentContacts == contactsCount){
                Toast.makeText(activity, "Update occurred", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(activity, "New contact added", Toast.LENGTH_SHORT).show();
            }

            contactsCount = currentContacts;

    }





    private int getContactCount() {
        Cursor cursor = null;
        try {
            cursor = activity.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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21650621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档