Android获取联系人

我们在实际生活中只能充当数据访问者,那么怎么访问手机中的“数据”呢?
所谓数据,在手机中指的就是联系人、信息、图片、音乐等等一些媒体文件,现在我就说说怎么获取手机中的联系人。
一、单纯只是获得手机联系人
1、写一个Activity,两个Button,然后使用ListView绑定数据
这里写图片描述
2、获取内容访问者:cr = getContentResolver();
3、然后再获取联系人

    public void getContactsDate(View view){
        //获取联系人(id,name)
        //获取要得到数据的路径URI
        Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
        //用数据访问者执行方法query,把路径放进去,得到一个Cursor游标
        Cursor cursor=cr.query(uri,null,null,null,null);
        //实例化一个数据集合
        list = new ArrayList<>();
        //实例化一个Map集合
        contactIdMap = new HashMap<Integer, ContactBean>();
        while(cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String name=cursor.getString(cursor.getColumnIndex("display_name"));

            //继续获取相对应联系人的数据
 //注意:一定要加content://协议,同时记得加权限   
 //    <uses-permission android:name="android.permission.READ_CONTACTS" />
        //content://com.android.contacts/raw_contacts/"+id+"/data
            Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
            Cursor cursorData=cr.query(uriData,null,null,null,null);

            while (cursorData.moveToNext()){
                String data1=cursorData.getString(cursorData.getColumnIndex("data1"));
//                int type=cursorData.getInt(cursorData.getColumnIndex("mimetype_id"));
                String type=cursorData.getString(cursorData.getColumnIndex("mimetype"));
                if(type.equals("vnd.android.cursor.item/phone_v2")){
                    Log.i("test","     "+data1+"/n"+type);
                    //写一个实体类ContactBean,new一个
                    ContactBean c=new ContactBean();
                    c.setDesplayName(name);
                    c.setPhoneNum(data1);
                    c.setPhotoId((long) id);
                    c.setLookUpKey(0+"");
                    //把对象放入实体类
                    list.add(c);
                    contactIdMap.put(0, c);
                }
            }
        }
        Log.i("ccc",list.size()+"");
        //实例化一个适配器,把集合放入适配器中
        adapter = new ContactListAdapter(this, list);
        listview.setAdapter(adapter);
    }

这样点击查看联系人信息,数据就绑出来了

然后点击ListView,进入手机系统内的联系人详细页面
3、先初始化数据库查询参数

private void init() {
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // 联系人Uri;
    // 查询的字段
    String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key",
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.PHOTO_ID,
            ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY };
    // 按照sort_key升序查詢
    asyncQueryHandler.startQuery(0, null, uri, projection, null, null,
            "sort_key COLLATE LOCALIZED asc");

}

2、先写一个内部类MyAsyncQueryHandler 然后继承AsyncQueryHandler 异步查询数据库类对象
private class MyAsyncQueryHandler extends AsyncQueryHandler {

    public MyAsyncQueryHandler(ContentResolver cr) {
        super(cr);
    }

    @Override
    protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
        if (cursor != null && cursor.getCount() > 0) {
            contactIdMap = new HashMap<Integer, ContactBean>();
            list = new ArrayList<ContactBean>();
            cursor.moveToFirst(); // 游标移动到第一项
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToPosition(i);
                String name = cursor.getString(1);
                String number = cursor.getString(2);
                String sortKey = cursor.getString(3);
                int contactId = cursor.getInt(4);
                Long photoId = cursor.getLong(5);
                String lookUpKey = cursor.getString(6);

                if (contactIdMap.containsKey(contactId)) {
                    // 无操作
                } else {
                    // 创建联系人对象
                    ContactBean contact = new ContactBean();
                    contact.setDesplayName(name);
                    contact.setPhoneNum(number);
                    contact.setSortKey(sortKey);
                    contact.setPhotoId(photoId);
                    contact.setLookUpKey(lookUpKey);
                    Log.i("ccc",photoId+"");
                    list.add(contact);

                    contactIdMap.put(contactId, contact);
                }
            }

        }

        super.onQueryComplete(token, cookie, cursor);
    }

}

4、点击事件
private void setAdapter(final List list) {

    adapter = new ContactListAdapter(this, list);
    contactList.setAdapter(adapter);

    contactList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            li = new ArrayList<ContactBean>();
            ContactBean c=new ContactBean();
            c.setPhotoId(list.get(position).getPhotoId());
            c.setPhoneNum(list.get(position).getPhoneNum());
            c.setDesplayName(list.get(position).getDesplayName());
            li.add(c);
            edit_querys.setText(list.get(position).getPhoneNum());

        }
    });
}    

*加入适配器后,就可以对你查出来的诗句进行点击,同时可以进到系统自带的联系人“详细页面”

二、怎么获取系统联系人的页面同时选择一个把选中的那一个绑定出来
1、首先先在当前页面写一个文本框接收那边传过来的值,然后点击跳转系统联系人页面执行一个getData的方法跳到系统联系人页面
public void getData(View view){

    Uri uri=Uri.parse("content://contacts/people");
    Intent intent=new Intent(Intent.ACTION_PICK,uri);
    startActivityForResult(intent,0);
}

2、接着点击系统联系人页面的联系人,然后得到被选中的人的姓名和电话号码拼接成一个字符串,将值返回到初始页面,并且绑定在写好的文本框中。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case 0:
if(data==null) {
return;
}
//处理返回的data,获取选择的联系人信息
Uri uri=data.getData();
String[] contacts=getPhoneContacts(uri);
s = contacts[0]+”:”+contacts[1];
bangding.setText(s);
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

private String[] getPhoneContacts(Uri uri){
    String[] contact=new String[2];
    //得到ContentResolver对象
    ContentResolver cr = getContentResolver();
    //取得电话本中开始一项的光标
    Cursor cursor=cr.query(uri,null,null,null,null);
    if(cursor!=null)
    {
        cursor.moveToFirst();
        //取得联系人姓名
        int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        contact[0]=cursor.getString(nameFieldColumnIndex);
        //取得电话号码
        String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
        if(phone != null){
            phone.moveToFirst();
            contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        }
        phone.close();
        cursor.close();
    }
    else
    {
        return null;
    }
    return contact;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值