質問

i am using a ContentObserver to listen for changes in the contacts database. now i realized that the onChange() method gets randomly called, even if i have not made any changes to the contacts. i suspect this is somehow related to the automatic contact syncing (even though there are no real changes to the contacts at this point).

is there a possibility to get notified only if there are real changes in the contacts, made by the user?

thx 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();
}
}
役に立ちましたか?

解決

ok, since no one seems to have an answer to this question, here is what i've done:

when creating the observer, i load all the contacts into a cache. then, on every onChange() event, i load the contacts again and compare them to the cached ones to see if there is a difference or not.

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top