手动更新联系人的ContentObserver [英] ContentObserver for contact update manually

查看:67
本文介绍了手动更新联系人的ContentObserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从服务中注册了ContentObserver,并且当电话中有更新(例如呼叫或联系人更新)时,我获得了onchange()功能.但是我希望仅在添加,更新或删除发生时调用onchange()函数.但是我不希望来电是呼入还是呼出.那么有人可以告诉我可以在contentObserver中注册哪个URI吗?我的代码在这里

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

和Contact_change.java类就像

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;
  }

}

修改:
我还有一个问题是,停止服务后,如果我更改了联系方式,还会调用onchange()函数.因此,我该如何停止或取消注册contentobserver.


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.

推荐答案

为了停止从ContentObserver接收通知,您必须注销它.

In order to stop receiving notifications from ContentObserver, you must unregister it.

创建ContentObserver的实例,以后可用于注册/注销.

Create an instance of ContentObserver which you can use later to register/unregister.

Contact_change changeObserver = new Contact_change();

注册观察者:

getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true,changeObserver);

取消注册观察者:

getContentResolver().unregisterContentObserver(changeObserver);

这篇关于手动更新联系人的ContentObserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆