0

是否可以过滤并允许用户仅从列表中选择联系人手机号码?

当前代码:

    case R.id.contacts_button:
            Intent contactsIntent = new Intent(Intent.ACTION_GET_CONTENT);
            contactsIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            startActivityForResult(contactsIntent, 1);
            return;

但这显示了所有类型的电话号码,包括传真、工作、家庭,我更愿意只看到手机号码。

4

1 回答 1

0

这将获取包含基本联系人数据的光标,并将循环访问联系人拥有的电话号码,可以有多个。

    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(); 
   } 
 }
于 2013-10-30T05:02:51.727 回答