要从联系人获取信息,可以使用Android的联系人内容提供器(ContentResolver)来查询联系人。以下是一个示例代码,演示如何从联系人获取电话号码:
// 在AndroidManifest.xml文件中添加读取联系人权限:
 phoneNumbers = new ArrayList<>();
// 查询联系人
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// 遍历联系人
if (cursor != null && cursor.getCount() > 0) {
    while (cursor.moveToNext()) {
        // 获取联系人的ID
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        // 获取联系人的姓名
        String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        // 查询联系人的电话号码
        Cursor phoneCursor = contentResolver.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[]{contactId},
                null);
        // 遍历电话号码
        if (phoneCursor != null && phoneCursor.getCount() > 0) {
            while (phoneCursor.moveToNext()) {
                String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                phoneNumbers.add(phoneNumber);
            }
        }
        // 关闭电话号码游标
        if (phoneCursor != null) {
            phoneCursor.close();
        }
    }
}
// 关闭联系人游标
if (cursor != null) {
    cursor.close();
}
// 打印电话号码列表
for (String phoneNumber : phoneNumbers) {
    Log.d("Contact", "Phone number: " + phoneNumber);
}
 这段代码使用了ContentResolver来查询联系人。首先,我们查询所有联系人的数据,并遍历每个联系人。对于每个联系人,我们获取其ID和姓名,并使用CONTACT_ID来查询其电话号码。最后,我们将电话号码存储在列表phoneNumbers中,并打印出来。
请确保在AndroidManifest.xml文件中添加了读取联系人权限,否则将无法成功查询联系人信息。