Android读取手机通讯录实现

Android读取手机通讯录实现

爪哇米工作室 陈跃峰

出自:http://blog.csdn.net/mailbomb

说明:转载请注明出处

 

         在Android开发中,读取手机通讯录中的号码是一种基本操作,但是由于Android的版本众多,所以手机通讯录操作的代码比较纷杂,在本文中进行一下总结。

         Android1.5是现在的Android系统中最低的版本,首先来说一下适用于Android1.5及以上版本(含2.X,3.X)的代码实现:

                   //获得所有的联系人  

       Cursor cur = context.getContentResolver().query(  

               Contacts.People.CONTENT_URI,  

                null,  

                null,  

                null,  

                Contacts.People.DISPLAY_NAME +" COLLATE LOCALIZED ASC");  

       // 循环遍历  

       if (cur.moveToFirst()) {

           int idColumn = cur.getColumnIndex(Contacts.People._ID);  

           int displayNameColumn = cur.getColumnIndex(Contacts.People.DISPLAY_NAME);  

           do {  

                // 获得联系人的ID号  

                String contactId =cur.getString(idColumn); 

               

                // 获得联系人姓名  

                String disPlayName =cur.getString(displayNameColumn);  

                

                                     //获取联系人的电话号码

                                     CursorphonesCur = context.getContentResolver().query(  

                                                        Contacts.Phones.CONTENT_URI,null,  

                                                        Contacts.Phones.PERSON_ID+ "=" + contactId, null, null);

                           if (phonesCur.moveToFirst()) {  

                               do {  

                                   // 遍历所有的电话号码  

                                        StringphoneType = phonesCur.getString(phonesCur  

                                           .getColumnIndex(Contacts.PhonesColumns.TYPE));

                                   String phoneNumber =phonesCur.getString(phonesCur  

                                           .getColumnIndex(Contacts.PhonesColumns.NUMBER));  

                                         //自己的逻辑处理代码

                               }while(phonesCur.moveToNext());  

                           }

           }while (cur.moveToNext());

}

cur.close();

         使用这段代码可以在各种版本的Android手机中读取手机通讯录中的电话号码,而且可以读取一个姓名下的多个号码,但是由于使用该代码在2.x版本中的效率不高,读取的时间会稍长一些,而且2.x现在是Android系统的主流,至少占有80%以上的Android手机份额,所以可以使用高版本的API进行高效的读取。

         适用于Android2.0及以上版本的读取通讯录的代码如下:

                   //读取手机本地的电话

              ContentResolver cr =context.getContentResolver();

              //取得电话本中开始一项的光标,必须先moveToNext()

              Cursor cursor =cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

              while(cursor.moveToNext()){

                  //取得联系人的名字索引

                  int nameIndex  =cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);

                  String name = cursor.getString(nameIndex);

                  //取得联系人的ID索引值

                  String contactId =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                  //查询该位联系人的电话号码,类似的可以查询email,photo

                  Cursor phone =cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,

                                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " 

                          + contactId, null, null);//第一个参数是确定查询电话号,第三个参数是查询具体某个人的过滤值

                  //一个人可能有几个号码

                  while(phone.moveToNext()){

                      String phoneNumber =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                      listName.add(name);

                      listPhone.add(phoneNumber);

                  }

                  phone.close();

              }

              cursor.close();

         如果需要读取SIM卡里面的通讯录内容,则可以使用:”content://icc/adn”进行读取,代码如下:

                   try{

                 Intent intent = new Intent();

                 intent.setData(Uri.parse(“content://icc/adn”));

                 Uri uri = intent.getData();

                      ContentResolvercr = context.getContentResolver();

                 Cursor cursor =context.getContentResolver().query(uri, null, null, null, null);

                 if (cursor != null) {

                          while(cursor.moveToNext()){

                       //取得联系人的名字索引

                       int nameIndex  = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);

                       String name = cursor.getString(nameIndex);

                       //取得联系人的ID索引值

                       String contactId =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                       //查询该位联系人的电话号码,类似的可以查询email,photo

                       Cursor phone =cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,

                                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " 

                               + contactId, null, null);//第一个参数是确定查询电话号,第三个参数是查询具体某个人的过滤值

                       //一个人可能有几个号码

                       while(phone.moveToNext()){

                           String phoneNumber =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                           //自己的逻辑代码

                       }

                       phone.close();

                   }

                   cursor.close();

                 }

             }catch(Exception e){}

 

希望对大家有所帮助!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
读取Android手机通讯录,可以使用Android提供的ContentResolver类和ContactsContract类。下面是一个简单的示例代码: ```java import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract; public class ContactReader { public static void readContacts(Context context) { ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; try { cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); if (phoneCursor != null && phoneCursor.moveToNext()) { String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); System.out.println("Name: " + name + ", Phone Number: " + phoneNumber); } if (phoneCursor != null) { phoneCursor.close(); } } } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } } } ``` 在上面的代码中,我们首先获取ContentResolver对象,然后使用它查询ContactsContract.Contacts.CONTENT_URI来获取所有联系人的ID和名称。接着,我们使用联系人ID查询ContactsContract.CommonDataKinds.Phone.CONTENT_URI来获取联系人的电话号码。最后,我们打印联系人的名称和电话号码。调用readContacts()方法即可读取手机通讯录

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值