获取联系人的所有电话号码

时间:2012-07-31 04:47:46

标签: android phone-number

我使用以下代码从联系人中检索所有电话号码。

Cursor c = context.getContentResolver().query(
                        Data.CONTENT_URI,
                        new String[] { Phone.NUMBER },
                        Data.MIMETYPE + "='" +     Phone.CONTENT_ITEM_TYPE
                                + "'", null, null);

它在我的Android手机中完美运行。

但是有些用户说我的应用程序没有从联系人获取所有电话号码,只是其中的一部分。我无法弄清楚原因..为什么?

1 个答案:

答案 0 :(得分:3)

这将使光标保持基本联系人数据,并将循环通过联系人拥有的电话号码,可以有多个。

Uri uri = data.getData();

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


while (cursor.moveToNext()) { 
   String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
   String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

   if (Boolean.parseBoolean(hasPhone)) { 
                    // You know have the number so now query it like this
    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));                 
        } 
        phones.close(); 
    } 
 }
相关问题