阅读联系电话号码对某些人而不是其他人有用吗?

时间:2014-11-04 04:18:27

标签: android android-contacts phone-number

我正在阅读Android中联系人列表中的联系人姓名和电话号码。我成功地读了这些名字。但是,当我按名称查找联系电话号码时(在这种情况下它们都是独一无二的,它只是一个测试),它只适用于一个联系人,没有其他人的电话号码,并获得一个错误的电话号码。

以下是我的getNumbers方法中的代码:

private String getNumber(String name){
    String returnMe="";
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null,
            "DISPLAY_NAME = '" + getIntent().getStringExtra("name") + "'", null, null);

    if(cursor.moveToFirst()){
        String identifier = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, ContactsContract.CommonDataKinds.Phone._ID + " = " + identifier, null, null);
        while(phones.moveToNext()){
            returnMe = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            switch(type){
                case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                    System.out.println("mobile number found"); break;
                default:
                    System.out.println("nothing found."); break;
            }
        }
    }
    return returnMe;
}

2 个答案:

答案 0 :(得分:2)

你做错了一些事情:

1)第二个查询必须以Data表为目标,而不是电话表:

Cursor phones = contentResolver.query(ContactsContract.Data.CONTENT_URI, ...

并指定您希望在where子句的DATA1列中使用电话号码:

Data.MIMETYPE = Phone.CONTENT_ITEM_TYPE

2)您需要通过RawContact的_ID

过滤结果

这会将Contact行的_ID与Phone行的_ID进行比较,这几乎没有机会相同:

ContactsContract.CommonDataKinds.Phone._ID + " = " + identifier

将Data.CONTACT_ID与光标的Contact._ID属性进行比较

Data.CONTACT_ID + " = " + identifier

Data javadoc页面上的示例提供了一个更完整的示例:

查找给定联系人的给定类型的所有数据

Cursor c = getContentResolver().query(Data.CONTENT_URI,
      new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
      Data.CONTACT_ID + "=?" + " AND "
              + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
      new String[] {String.valueOf(contactId)}, null);

答案 1 :(得分:0)

见我的回答:

ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
        //Query phone here
      if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
        Cursor pCur = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null);
        while (pCur.moveToNext()) {
        // Get phone numbers here
        } 
        pCur.close();
    }
  }
 }
}