2012-07-31 127 views
0

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

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

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

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

+0

的[阅读所有联系人的电话号码储存在机器人]可能重复(http://stackoverflow.com/questions/2356084/read-all-contacts-phone-numbers-in-android) – Praveenkumar 2012-07-31 05:00:45

+0

号我的问题这就是为什么它不能很好地工作。根据http://developer.android.com/intl/zh-CN/reference/android/provider/ContactsContract.Data.html – 2012-07-31 05:39:19

回答

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(); 
    } 
} 
+0

它应该工作得很好。但是,如果我只想要所有电话号码,该方法太慢。无论如何,谢谢你的回复。 – 2012-07-31 05:41:52

相关问题