问题
I have registered a ContentObserver
from service and I get the onchange()
function when there is update in phone like call or contact update. But I want the onchange()
function to be called only when add, update or delete happens. But I don't want when call is incoming or outgoing. So can anybody tell me which URI
I can register in contentObserver
? My code is here
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true,new Contact_change());
and Contact_change.java class is like
public class Contact_change extends ContentObserver{
public Contact_service() {
super(null);
}
@Override
public void onChange(boolean selfChange){
Log.i("contact_service","onchange");
Super.onChange(selfChange);
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
}
Edit:
I have one more problem is that after stop the service if I made change in contact then also onchange()
function is called. So how can I stop that or un register the contentobserver
.
回答1:
I used the ContactsContract.Contacts.CONTENT_VCARD_URI as mentioned here.
And also you could set a threshold_time like the one mentioned here
Its a bit more efficient.
回答2:
In order to stop receiving notifications from ContentObserver, you must unregister it.
Create an instance of ContentObserver which you can use later to register/unregister.
Contact_change changeObserver = new Contact_change();
Register observer:
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true,changeObserver);
Unregister observer:
getContentResolver().unregisterContentObserver(changeObserver);
来源:https://stackoverflow.com/questions/12244140/contentobserver-for-contact-update-manually