如何让我的的Andr​​oid ContentObserver用于检测被更改的联络方式(添加,更新或删除)? [英] How do I make my Android ContentObserver for detect changed contact detail (added, updated or deleted)?

查看:172
本文介绍了如何让我的的Andr​​oid ContentObserver用于检测被更改的联络方式(添加,更新或删除)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当接触被修改我能赶上活动。但我要赶上像CONTACT_ID,姓名等修改联系方式......请你能帮我做到这一点。我的code是跟随。

 公共类TestContentObserver延伸活动{
    INT contactCount = 0;
    最终的String [] =投影新的String [] {RawContacts.CONTACT_ID,RawContacts.DELETED};
    光标CURVAL;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        CURVAL = getContentResolver()查询(RawContacts.CONTENT_URI,投影,NULL,NULL,NULL);
        contactCount = curval.getCount();        curval.registerContentObserver(新ContentObserver(新处理程序()){
            @覆盖
            公共无效的onChange(布尔selfChange){
                getChangedContactDetails();
            }
            @覆盖
            公共布尔deliverSelfNotifications(){
                返回true;
            }
        });    }    公共无效getChangedContactDetails(){
        //我怎么能赶上受灾联系方式
    }
}


解决方案

有一个看看:的 http://developer.android.com/resources/articles/contacts.html ,部分查找URI

您注册ContentObserver时,你可以注册一个URI直接指向你感兴趣的接触。那么当你的有趣联系人修改您的观察者才会得到通知。

 乌里lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI,lookupKey);
。getContentResolver()registerContentObserver(lookupUri,假的,新ContentObserver(){});

查找关键的关键是独一无二的每个记录

I can catch the event when contact is modified. But I want to catch that modified contact details like CONTACT_ID, name, etc... Please can you help me to do that. my code is follow.

public class TestContentObserver extends Activity {
    int contactCount = 0;
    final String[] projection = new String[] { RawContacts.CONTACT_ID, RawContacts.DELETED };
    Cursor curval;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        curval = getContentResolver().query(RawContacts.CONTENT_URI, projection, null, null, null);
        contactCount = curval.getCount();

        curval.registerContentObserver(new ContentObserver(new Handler()) {
            @Override
            public void onChange(boolean selfChange) {
                getChangedContactDetails();
            }
            @Override
            public boolean deliverSelfNotifications() {
                return true;
            }
        });

    }



    public void getChangedContactDetails(){
        // how can I catch the affected contact details
    }
}

解决方案

Have a look at: http://developer.android.com/resources/articles/contacts.html, section "Lookup URI"

when registering your ContentObserver you can just register an URI that points directly to the contact you are interested in. then your observer will only get notified when your interesting contact is modified.

Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
getContentResolver().registerContentObserver(lookupUri, false, new ContentObserver(){});

Lookup key key is unique for each record

这篇关于如何让我的的Andr​​oid ContentObserver用于检测被更改的联络方式(添加,更新或删除)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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