0

我尝试从电话中检索联系人的姓名和号码,但它仅从联系人号码中检索 4 到 5 个联系人。带着敬意

我用这个。

cContact = getContentResolver().query(People.CONTENT_URI, new String[] {People.NAME,People.NUMBER},null, null, null);
           cContact.moveToFirst();
             while(!cContact.isLast())
                {
                 str=null;
                 str=cContact.getString(cContact.getColumnIndex(People.NAME));
                 str=str+"|"+cContact.getString(cContact.getColumnIndex(People.NUMBER));


                 Toast.makeText(getBaseContext(),str,Toast.LENGTH_SHORT).show();
                 outContact.append(str);
                   outContact.append("\r\n");
                   cContact.moveToNext();


               }//while
4

1 回答 1

1

People 类目前已被弃用。您必须使用ContactsConstract

有一个例子:

/**
 * Research all contacts from "Contacts" table
 * @return List<Contact>
 */
public List<Contact> getAllContacts() {
    List<Contact> returnList = new ArrayList<Contact>();

    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.STARRED,
    };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + "1" + "'";;
    String[] selectionArgs = null;
    String sortOrder = /*ContactsContract.Contacts.STARRED + " DESC , " +*/  ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    Cursor cursor = _context.getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);

    cursor.moveToFirst();
    //For each contact
    while(!cursor.isAfterLast()) {
        Integer columnID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
        Integer columnName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        Integer columnStarred = cursor.getColumnIndex(ContactsContract.Contacts.STARRED);

        Contact contact = new Contact();
        contact.setId(cursor.getInt(columnID));
        contact.setName(cursor.getString(columnName));
        contact.setFavourite(Boolean.parseBoolean(cursor.getString(columnStarred)));

        returnList.add(contact);

        cursor.moveToNext();
    }

    return returnList;      
}

要检索电话号码,您应该使用另一个 CONTENT_URI。

/**
 * Return all the phones for a specific contact
 * @param id Row "CONTACT_ID" of the table Contacts
 * @return Map<Integer, String>
 */
private SparseArray<String> getPhones(Integer id) {
    SparseArray<String> phonesMap = new SparseArray<String>();

    //Research all the phones number
    Cursor phones = _context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, null, null); 

    Log.d(this.getClass().getName(),String.valueOf(phones.getCount()));

    phones.moveToFirst(); 

    //Foreach phone number
    while(!phones.isAfterLast()) {
        String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));               
        int phoneType = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

        if(phoneType==ContactsContract.CommonDataKinds.Phone.TYPE_HOME) {
            phonesMap.put(ContactsContract.CommonDataKinds.Phone.TYPE_HOME, phoneNumber);
        }
        if(phoneType==ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
            phonesMap.put(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, phoneNumber);
        }
        if(phoneType==ContactsContract.CommonDataKinds.Phone.TYPE_WORK) {
            phonesMap.put(ContactsContract.CommonDataKinds.Phone.TYPE_WORK, phoneNumber);
        }

        phones.moveToNext();
    }
    phones.close(); 

    return phonesMap;
}
于 2012-12-19T08:09:30.600 回答