【问题标题】:ContactsContract lookup Phone Number by Contact IdContactsContract 按联系人 ID 查找电话号码
【发布时间】:2011-06-11 09:38:38
【问题描述】:

我正在尝试通过联系人 ID 查找联系人的电话号码,结果正在返回 - 电话号码:1

我尝试过使用 SO 周围的其他示例,但光标上的计数一直为 0

Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, contactId);

Log.i(TAG, "Locate Contact by Id: " + contactId + " at Uri: " + uri.toString());

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

try {
    if (cursor.moveToFirst()) {
        Log.i(TAG, "Phone Number: " + cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));
    }
} finally {
    cursor.close();
}

【问题讨论】:

    标签: android uri contactscontract


    【解决方案1】:

    试试这个:

    private ArrayList<String> getPhoneNumbers(String id) 
    {
        ArrayList<String> phones = new ArrayList<String>();
    
        Cursor cursor = mContentResolver.query(
                CommonDataKinds.Phone.CONTENT_URI, 
                null, 
                CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                new String[]{id}, null);
    
        while (cursor.moveToNext()) 
        {
            phones.add(cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER)));
        } 
    
        cursor.close();
        return(phones);
    }
    

    【讨论】:

      【解决方案2】:

      我最终是通过Phone.LOOKUP_KEY 而不是Phone.CONTACT_ID 完成的;

      private HashMap<String, CharSequence> lookupPhoneNumbers(String lookupKey)
      {
          HashMap<String, CharSequence> numbers = new HashMap<String, CharSequence>();
      
          Cursor cursor = getContext().getContentResolver().query(Phone.CONTENT_URI, null, Phone.LOOKUP_KEY + " = ?", new String[] { lookupKey }, null);
          try
          {
              while (cursor.moveToNext())
              {
                  String phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
                  int type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
                  CharSequence phoneLabel = Phone.getTypeLabel(getResources(), type, "Undefined");
                  // boolean isPrimary = (cursor.getInt(cursor.getColumnIndex(Phone.IS_PRIMARY)) == 1);
      
                  numbers.put(phoneNumber, phoneLabel);
              }
          } finally
          {
              cursor.close();
          }
      
          return numbers;
      }
      

      【讨论】:

      • 您使用的查找键是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      相关资源
      最近更新 更多