查询手机联系人和sim卡联系人

获得手机通讯录联系人信息
  
  
  1.  /**得到手机通讯录联系人信息**/  
  2.    private void getPhoneContacts() {  
  3. ContentResolver resolver = mContext.getContentResolver();  
  4.  
  5. // 获取手机联系人  
  6. Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);  
  7.  
  8.  
  9. if (phoneCursor != null) {  
  10.     while (phoneCursor.moveToNext()) {  
  11.  
  12.     //得到手机号码  
  13.     String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);  
  14.     //当手机号码为空的或者为空字段 跳过当前循环  
  15.     if (TextUtils.isEmpty(phoneNumber))  
  16.         continue;  
  17.       
  18.     //得到联系人名称  
  19.     String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);  
  20.       
  21.     //得到联系人ID  
  22.     Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);  
  23.  
  24.     //得到联系人头像ID  
  25.     Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);  
  26.       
  27.     //得到联系人头像Bitamp  
  28.     Bitmap contactPhoto = null;  
  29.  
  30.     //photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的  
  31.     if(photoid > 0 ) {  
  32.         Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);  
  33.         InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);  
  34.         contactPhoto = BitmapFactory.decodeStream(input);  
  35.     }else {  
  36.         contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);  
  37.     }  
  38.       
  39.     mContactsName.add(contactName);  
  40.     mContactsNumber.add(phoneNumber);  
  41.     mContactsPhonto.add(contactPhoto);  
  42.     }  
  43.  
  44.     phoneCursor.close();  
  45. }  
  46.    } 



获得手机sim卡联系人信息

sim卡和手机本人 获取的方式类似 只是url有点不一样 ,须要注意的一点是 sim卡 是没有联系人头像的。

 

  
  
  1.    /**得到手机SIM卡联系人人信息**/  
  2.    private void getSIMContacts() {  
  3. ContentResolver resolver = mContext.getContentResolver();  
  4. // 获取Sims卡联系人  
  5. Uri uri = Uri.parse("content://icc/adn");  
  6. Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,  
  7.     null);  
  8.  
  9. if (phoneCursor != null) {  
  10.     while (phoneCursor.moveToNext()) {  
  11.  
  12.     // 得到手机号码  
  13.     String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);  
  14.     // 当手机号码为空的或者为空字段 跳过当前循环  
  15.     if (TextUtils.isEmpty(phoneNumber))  
  16.         continue;  
  17.     // 得到联系人名称  
  18.     String contactName = phoneCursor 
  19.         .getString(PHONES_DISPLAY_NAME_INDEX);  
  20.  
  21.     //Sim卡中没有联系人头像  
  22.       
  23.     mContactsName.add(contactName);  
  24.     mContactsNumber.add(phoneNumber);  
  25.     }  
  26.  
  27.     phoneCursor.close();  
  28. }  
  29.    } 
这个界面就可以看到联系人的 名称 号码 以及头像了。如果想在模拟器上看须要将图片拷贝到SD卡中,然后在联系人中设置一下,这里就可以看到头像了,或者 真机上会比较清楚、
 

 

任意点击一个联系人会调用系统拨打电话的界面 ,代码如下。
    
    
  1. //调用系统方法拨打电话  
  2. Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri  
  3.     .parse("tel:" + mContactsNumber.get(position)));  
  4. startActivity(dialIntent); 




最重要的是须要AndroidManifest.xml中 加入权限 否则代码会报错的。 千万别忘了。

 

    
    
  1. <!-- 读取联系人权限 -->   
  2. <uses-permission android:name="android.permission.READ_CONTACTS"/> 
  3. <!-- 拨打电话权限 --> 
  4. <uses-permission android:name="android.permission.CALL_PHONE"/>   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值