5

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();
}
}
4

1 に答える 1

1

わかりました、誰もこの質問に対する答えを持っていないようなので、これが私がやったことです:

オブザーバーを作成するときに、すべての連絡先をキャッシュに読み込みます。次に、すべての onChange() イベントで、連絡先を再度読み込み、それらをキャッシュされたものと比較して、違いがあるかどうかを確認します。

最もエレガントなソリューションではありませんが、少なくとも機能します...

于 2012-02-16T21:21:08.253 に答える