Retrieve Contact Phone Number From URI in Android

后端 未结 2 1188
离开以前
离开以前 2020-11-29 09:01

I am trying to get the contact\'s phone number after I have retrieved their ID number from the built-in activity. However, whenever I query the database using the cursor in

2条回答
  •  旧巷少年郎
    2020-11-29 09:35

    This should work, (maybe try losing the type)

    Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.

            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()) {
            // Do something with phones
            } 
            pCur.close();
        }
    

    Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.

提交回复
热议问题