阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

老司机
查看: 1144|回复: 0

[转帖] android获取联系人姓名和电话号码,1.5以上兼容

[复制链接]
  • TA的每日心情
    开心
    2012-3-7 10:15
  • 签到天数: 11 天

    [LV.3]偶尔看看II

    发表于 2012-2-1 10:59:58 | 显示全部楼层 |阅读模式
    HashMap<String, String> contacts = new HashMap<String, String>();
                    //联系人兼容问题处理
                    String contactUri="content://contacts/people";//联系人读取地址
                    String name="display_name";//联系人姓名列名
                    String number="number";//联系人电话;
                    if(Integer.valueOf(android.os.Build.VERSION.SDK)>=4){
                            number="data1";
                            contactUri="content://com.android.contacts/contacts";
                    }
                    Cursor cursor = null;
                    Cursor phone=null;
                    try {
                            // 1.5获取号码
                            cursor = context.getContentResolver().query(
                                            Uri.parse(contactUri), null, null, null, null);
                            cursor.moveToFirst();
                            while (!cursor.isAfterLast()) {
                                    //联系人姓名
                                    String peopleName=cursor.getString(cursor.getColumnIndex(name));
                                    //联系人编号
                                    String cid = cursor.getString(cursor.getColumnIndex("_id"));
                                    //获取用户所有号码
                                    if(Integer.valueOf(android.os.Build.VERSION.SDK)>=4){
                                            phone= context.getContentResolver().query(
                                                            Uri.parse("content://com.android.contacts/data/phones"), new String[]{number},"contact_id="+cid, null, null);
                                    }
                                    else{
                                            Uri personUri = ContentUris.withAppendedId(Contacts.People.CONTENT_URI,
                                                          Long.parseLong(cid));
                                            //用户联系方式地址
                                            Uri phonesUri = Uri.withAppendedPath(personUri,
                                                            Contacts.People.Phones.CONTENT_DIRECTORY);
                                              phone = context.getContentResolver().query(phonesUri,
                                                              new String[]{number}, null, null, null);
                                    }
                                while(phone.moveToNext()){
                                        //电话号码
                                        String phoneNumber=phone.getString(0);
                                        //要显示的信息
                                        String key=peopleName+"("+phoneNumber+")";
                                        if(!contacts.containsKey(key)){
                                                contacts.put(key, phoneNumber);
                                        }
                                }
                                phone.close();
                                    cursor.moveToNext();
                            }
                    } finally {
                            if (cursor != null) {
                                    cursor.close();
                                    cursor = null;
                            }
                            if(phone!=null){
                                    phone.close();
                                    phone=null;
                            }
                    }
                    return contacts;
    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表