联系方式ContentObserver所谓随机 [英] Contacts ContentObserver called randomly

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

问题描述

我使用的是ContentObserver侦听更改联系人数据库。现在我认识到的onChange()方法被随机调用,即使我还没有做出任何更改的联系人。我怀疑这是某种相关的自动联系人同步(即使没有真正的改变触点在这一点)。

有没有可能得到通知仅当有在接触真正的改变,用户所做的?

THX西门

 公共类ContactsObserver扩展ContentObserver {
私人最终静态字符串变量= ContactsObserver.class.getSimpleName();

私人上下文CTX;
私人列表< ContactsChangeListener>听众=新的ArrayList< ContactsChangeListener>();

私人ContactsObserver(上下文CTX){
    超级(新处理程序());
    this.ctx = ctx.getApplicationContext();
    ctx.getContentResolver()
        .registerContentObserver(
                ContactsContract.Contacts.CONTENT_URI,// URI
                假的,// notifyForDescendents
                本); //观察员
}

@覆盖
公共无效的onChange(布尔selfChange){
    Log.i(TAG,针数变);
    对于(ContactsChangeListener L:听众){
        l.onContactsChange();
    }
}

@覆盖
公共布尔deliverSelfNotifications(){
    返回false; //设置为true,并不能改变什么?
}

公共静态ContactsObserver寄存器(上下文CTX){
    Log.d(TAG,注册);
    返回新ContactsObserver(CTX);
}

公共无效注销(){
    Log.d(TAG,注销);
    。ctx.getContentResolver()unregisterContentObserver(本);
}

公共无效addContactsChangeListener(ContactsChangeListener L){
    listeners.add(升);
}

公共接口ContactsChangeListener {
    无效onContactsChange();
}
}
 

解决方案

确定,因为似乎没有人有这个问题的答案,这里是我做了什么:

创建观察者的时候,我加载所有接触到缓存中。然后,在每一个的onChange()事件,我再次加载的联系,把它们比作缓存的人,看看是否是有差别与否。

不是最完美的解决方案,但它至少...

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...

这篇关于联系方式ContentObserver所谓随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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