ContentObserver contacts are called randomly

I am using ContentObserver to listen for changes to the contacts database. now I realized that the onChange () method gets a random name, even if I haven't made any changes to the contacts. I suspect that this is somehow connected with the automatic synchronization of contacts (even if at the moment there are no real changes in the contacts).

is it possible to receive notifications only if there are real changes in the contacts made by the user?

thanks simon

public class ContactsObserver extends ContentObserver { private final static String TAG = ContactsObserver.class.getSimpleName(); private Context ctx; private List<ContactsChangeListener> listeners = new ArrayList<ContactsChangeListener>(); private ContactsObserver(Context ctx) { super(new Handler()); this.ctx = ctx.getApplicationContext(); ctx.getContentResolver() .registerContentObserver( ContactsContract.Contacts.CONTENT_URI, // uri false, // notifyForDescendents this); // observer } @Override public void onChange(boolean selfChange) { Log.i(TAG, "Contacs change"); for(ContactsChangeListener l : listeners){ l.onContactsChange(); } } @Override public boolean deliverSelfNotifications() { return false; // set to true does not change anything... } public static ContactsObserver register(Context ctx){ Log.d(TAG, "register"); return new ContactsObserver(ctx); } public void unregister(){ Log.d(TAG, "unregister"); ctx.getContentResolver().unregisterContentObserver(this); } public void addContactsChangeListener(ContactsChangeListener l){ listeners.add(l); } public interface ContactsChangeListener{ void onContactsChange(); } } 
+4
source share
1 answer

ok, since no one has the answer to this question, here is what I did:

when creating an observer, I load all the contacts into the cache. then in each onChange () event, I load the contacts again and compare them with the cached ones to see if there is a difference or not.

not the most elegant solution, but it works at least ...

+1
source

Source: https://habr.com/ru/post/1385608/


All Articles