Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

android - ContentObserver should call if and only if ContactsContract.Contacts.CONTENT_URI changes

As my application uses content from android.provider.ContactsContract.Data (API > 11) and ContactsContract.Contacts.CONTENT_URI (API < 11) to populate Contacts.

I've tried to registerContentObserver() against these provider. But it calls my ContentObserver even if I tries to Call a person from device as soon as I put the call. It does trigger my ContentObserver which is not useful for me as there's no Content Change in Contacts Provider.

Root Cause:

Seems like LAST_TIME_CONTACTED or something in ContactsContract.Contacts.CONTENT_URI do changes when a call has been made from device which legitimate wokes up by ContentObserver.

Tried:

private class ContactsContentObserver extends ContentObserver {
    public ContactsContentObserver() {
        super(null);
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Zname.getPreferences().setRefreshContact(true);
    }
}

Registered ContentObserver in OnCreate() of Activity

ContactsContentObserver contactsContentObserver = new ContactsContentObserver();
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, false, contactsContentObserver);

Tried with notifyForDescendents as false on registerContentObserver. Still it triggers out ContentObserver

Ques:

How can one register ContentObserver which triggers if and only if contacts information is under CRUD(Create,Update,Delete) except Last_Time_Contacted or its descendants?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The fundamental problem here is that registering for ContactsContract.Contacts.CONTENT_URI isn't working as you might reasonably think.

The reason why you get updates even if notifyForDescendents is false is because the Uri triggering the update is... ContactsContract.Contacts.CONTENT_URI and not the contact row that is being dialled.

The offending code in the Contacts app can be found at GrepCode and there is a bug filed for this on Google Code.

So to answer your question, you can't register a ContentObserver which will trigger for specific fields on a contact. You would need to have something in your app which will keep track of calculating the differences whenever onChange fires.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...