当前位置:首页 > 开发 > 移动开发 > 正文

android 获取手机通讯录信息

发表于: 2011-07-18   作者:dengzh   来源:转载   浏览次数:
摘要: protected void getContactInfo() { //获得通讯录信息 ,URI是ContactsContract.Contacts.CONTENT_URI Cursor cursor = getContentResolver().query(           

protected void getContactInfo() {

//获得通讯录信息 ,URI是ContactsContract.Contacts.CONTENT_URI

Cursor cursor = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {

//获得通讯录中每个联系人的ID
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));

//获得通讯录中联系人的名字
            String name = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            Log.v(TAG, "…name…" + name);

//查看给联系人是否有电话,返回结果是String类型,1表示有,0表是没有
            String hasPhone = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if (hasPhone.equalsIgnoreCase("1"))
                hasPhone = "true";
            else
                hasPhone = "false";

//如果有电话,根据联系人的ID查找到联系人的电话,电话可以是多个           

if (Boolean.parseBoolean(hasPhone)) {
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                while (phones.moveToNext()) {
                    String phoneNumber = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.v(TAG, "…phoneNumber…  " + phoneNumber);
                }
                phones.close();
            }

//查找email地址,这里email也可以有多个

            Cursor emails = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "
                            + contactId, null, null);
            while (emails.moveToNext()) {
                String emailAddress = emails
                        .getString(emails
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.v(TAG, "…emailAddress…  " + emailAddress);
            }
            emails.close();

//获得联系人的地址         

  Cursor address = getContentResolver()
                    .query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID
                                    + " = " + contactId, null, null);
            while (address.moveToNext()) {
                // These are all private class variables, don’t forget to create
                // them.
                String poBox = address
                        .getString(address
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                String street = address
                        .getString(address
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                String city = address
                        .getString(address
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                String state = address
                        .getString(address
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                String postalCode = address
                        .getString(address
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                String country = address
                        .getString(address
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                String type = address
                        .getString(address
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
                Log.v(TAG, "…city…  " + city);
            }
        }
        cursor.close();
    }

 

 

 

 

另一种方法

 

1。通讯录上的信息,存储在两个地方,一个是SIM卡,一个是手机本地,

首先是手机本地:Cursor cursor = getContentResolver().query(People.CONTENT_URI, null,
     null, null, null);
   while (cursor.moveToNext()) {
    ContactInfo cci = new ContactInfo();
    //取得联系人名字
    int nameFieldColumnIndex = cursor.getColumnIndex(People.NAME);
    cci.contactName = cursor.getString(nameFieldColumnIndex);
    //取得电话号码
    int numberFieldColumnIndex = cursor.getColumnIndex(People.NUMBER);
    cci.userNumber = cursor.getString(numberFieldColumnIndex);
    cci.userNumber = GetNumber(cci.userNumber);
    cci.isChecked = false;
    if (IsUserNumber(cci.userNumber)) {
     if (!IsContain(contactList, cci.userNumber)) {
      if(IsAlreadyCheck(wNumStr, cci.userNumber)){
       cci.isChecked = true;
       numberStr += "," + cci.userNumber;
      }
      contactList.add(cci);
      //Log.i("eoe", "*********"+cci.userNumber);
     }
    }
   }
   cursor.close();
}

下面是获取SIM卡:

//从SIM卡中取号
private void GetSimContact(String add){
   //读取SIM卡手机号,有两种可能:content://icc/adn与content://sim/adn
   try {
    Intent intent = new Intent();
    intent.setData(Uri.parse(add));
    Uri uri = intent.getData();
    mCursor = getContentResolver().query(uri, null, null, null, null);
    if (mCursor != null) {
     while (mCursor.moveToNext()) {
      ContactInfo sci = new ContactInfo();
      // 取得联系人名字
      int nameFieldColumnIndex = mCursor.getColumnIndex("name");
      sci.contactName = mCursor.getString(nameFieldColumnIndex);
      // 取得电话号码
      int numberFieldColumnIndex = mCursor
        .getColumnIndex("number");
      sci.userNumber = mCursor.getString(numberFieldColumnIndex);

      sci.userNumber = GetNumber(sci.userNumber);
      sci.isChecked = false;
     
      if (IsUserNumber(sci.userNumber)) {
       if (!IsContain(contactList, sci.userNumber)) {
        if(IsAlreadyCheck(wNumStr, sci.userNumber)){
         sci.isChecked = true;
         numberStr += "," + sci.userNumber;
        }
        contactList.add(sci);
        //Log.i("eoe", "*********"+sci.userNumber);
       }
      }
     }
     mCursor.close();
    }
   } catch (Exception e) {
    Log.i("eoe", e.toString());
   }
}

以上是将获取到的信息对象方法ArrayList<ContactInfo> contactList里面,然后砸显示的是采用适配器,这样就完成了

在写的时候,一定要注意获取的方式

android 获取手机通讯录信息

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
参考:http://www.2cto.com/kf/201409/336307.html 第一步:我们可以在File Explorer视图下找到cont
Android软件开发之获取通讯录联系人信息 雨松MOMO原创文章如转载,请注明:转载自雨松MOMO的博客原
Android软件开发之获取通讯录联系人信息(二十九) [复制链接] <table style="border-collapse:
读取全部的通讯录,但是过滤掉黑名单中已经存在的记录,其中过滤条件必须输入为People._id,不然会
打开contacts.db后 发面里面有一堆表,同学们先别慌张。今天我们主要讨论红框内的4个比较常用的表,
contentProvider获取通讯录信息,前提是你的模拟器通讯录中有数据。 1、activity_main.xml(用于显
//获取联系人 Uri rawContacts = Uri.parse("content://com.android.contacts/raw_contacts"); Cont
简洁大方的android手机通讯录,可以对联系人进行查看,搜索,快速导航,编辑,新增,删除,对指定联
由于工作需要,研究了一下android上获取内存信息的方法,总结如下: 1.SDK获取 在Java层利用API获取
在android开发中,有时候我们想获取手机的一些硬件信息,比如android手机的总内存和可用内存大小。
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号