根据姓名获取联系人的电话号码

public synchronized static ArrayList getPhoneByName(Context context, String name) {
    ArrayList numbers = new ArrayList();
    String number=null;
    ContentResolver resolver = context.getContentResolver();
    String[] projection=new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
    Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + "=?",
            new String[]{name}, null);
    if (cursor != null) {
        while (cursor.moveToNext()) {
             number= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            if (number!=null&&number!="") {
                numbers.add(number);
            }
        }
    }
    return numbers;
}

你可能感兴趣的:(android)