从联系人列表

时间:2016-01-14 08:49:47

标签: java android contacts

编辑:已回答 我正在使用以下代码从联系人列表中检索联系电话号码

Intent GetConactIntent = new Intent(Intent.ACTION_PICK, 
                         ContactsContract.Contacts.CONTENT_URI);
                 startActivityForResult(GetConactIntent, PICK_CONTACT);
@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data){ 
    super.onActivityResult(reqCode, resultCode, data);

switch(reqCode)
{
   case (PICK_CONTACT):
     if (resultCode == Activity.RESULT_OK)
     {
         Uri contactData = data.getData();
         Cursor c = managedQuery(contactData, null, null, null, null);
          if (c.moveToFirst())
          {
              String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

              String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

              if (hasPhone.equalsIgnoreCase("1")) 
              {
                  Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                  phones.moveToFirst();
                  //PhoneNumber
                  String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();

                  //ContactName
                   String nameContact = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                  TextView txtNum =(TextView)VodafoneActivity.this.findViewById(R.id.editTextNum);

                  //Execute
                  txtNum.setText( cNumber);
              }
         }
   }
}

}

但此代码仅查看联系人姓名,当我选择联系人时,它会选择联系人详细信息中的第一个号码,但在某些情况下,联系人的号码超过号码,我希望用户能够选择号码。
有什么想法吗?

我需要这样的东西:

Image Description for what I need

2 个答案:

答案 0 :(得分:0)

如果此代码无效,请尝试this

Cursor cursor = null;
try {
    cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
    int contactIdIdx = cursor.getColumnIndex(Phone._ID);
    int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
    int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
    int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
    cursor.moveToFirst();
    do {
        String idContact = cursor.getString(contactIdIdx);
        String name = cursor.getString(nameIdx);
        String phoneNumber = cursor.getString(phoneNumberIdx);
        //...
    } while (cursor.moveToNext());  
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (cursor != null) {
        cursor.close();
    }
}

答案 1 :(得分:0)

找到了它 电话号码存储在自己的表中,需要单独查询。要查询电话号码表,请使用存储在SDK变量ContactsContract.CommonDataKinds.Phone.CONTENT_URI中的URI。

Intent intent = new Intent(Intent.ACTION_PICK, CommonDataKinds.Phone.CONTENT_URI);