欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现读取、搜索联系人的代码

程序员文章站 2022-03-23 14:10:45
代码很简单,就不多废话了 复制代码 代码如下: //读取联系人 public static uri contactsuri = contactscontract.co...

代码很简单,就不多废话了

复制代码 代码如下:

//读取联系人
public static uri contactsuri = contactscontract.contacts.content_uri;//联系人
    public static void getcontactsinfo(context context,string tag){
        string[] projections = new string[]{contactscontract.contacts._id,contactscontract.contacts.display_name};
        cursor cursor = context.getcontentresolver().query(contactsuri, projections, null, null, null);
        int albumindex = cursor.getcolumnindexorthrow(projections[0]);
        int titleindex = cursor.getcolumnindexorthrow(projections[1]);
        log.d(tag, cursor.getcount()+"");
        while(cursor.movetonext()){
            string album = cursor.getstring(albumindex);
            string title = cursor.getstring(titleindex);
            log.d(tag, album+":"+title);
        }
        cursor.close();
    }
    
    //根据联系人搜索联系人信息
    public static void searchcontacts(context context,string tag){
        string searchname = "wang";
        uri uri = uri.withappendedpath(contactscontract.contacts.content_filter_uri, searchname);
        
    //  uri uri2 = uri.withappendedpath(contactscontract.phonelookup.content_filter_uri, phonenumber); 根据电话号码查找联系人
        
        string[] projection = new string[]{contactscontract.contacts._id};
        cursor cursor = context.getcontentresolver().query(uri, projection, null, null, null);
        string id = null;
        if (cursor.movetofirst()) {
            id = cursor.getstring(cursor.getcolumnindexorthrow(contactscontract.contacts._id));
        }
        cursor.close();
        if (id!=null) {
            string where = contactscontract.data._id+"="+id;
            projection = new string[]{contactscontract.data.display_name,contactscontract.commondatakinds.phone.number};
            cursor searchccursor = context.getcontentresolver().query(contactscontract.data.content_uri, projection, where, null, null);
            log.d(tag, searchccursor.getcount()+"");
            int nameindex = searchccursor.getcolumnindex(projection[0]);
            int numberindex = searchccursor.getcolumnindex(projection[1]);
            while(searchccursor.movetonext()){
                string name = searchccursor.getstring(nameindex);
                string number = searchccursor.getstring(numberindex);
                log.d(tag, number+":"+name);
            }
            searchccursor.close();
        }
    }

以上就是本文给大家分享的代码的全部内容了,希望大家能够喜欢。