基于Android名称从联系人获取电话号码

基于Android名称从联系人获取电话号码,android,contacts,fetch,phone-number,Android,Contacts,Fetch,Phone Number,我正在开发一个应用程序,在这里我需要从与提供的姓名对应的联系人中获取电话号码。我试过很多代码,但似乎都不管用 这是我目前正在使用的代码 public static String getContactPhoneNumber(Context context, String contactName, int type) { String phoneNumber = null; String[] whereArgs = new String[] { contactNam

我正在开发一个应用程序,在这里我需要从与提供的姓名对应的联系人中获取电话号码。我试过很多代码,但似乎都不管用

这是我目前正在使用的代码

public static String getContactPhoneNumber(Context context, String contactName, int type) {
        String phoneNumber = null;

        String[] whereArgs = new String[] { contactName, String.valueOf(type) };

        Log.d(TAG, String.valueOf(contactName));

        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.Contacts.DISPLAY_NAME + " = ? and "
                        + ContactsContract.CommonDataKinds.Phone.TYPE + " = ?", whereArgs, null);

        int phoneNumberIndex = cursor
                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

        Log.d(TAG, String.valueOf(cursor.getCount()));

        if (cursor != null) {
            Log.v(TAG, "Cursor Not null");
            try {
                if (cursor.moveToNext()) {
                    Log.v(TAG, "Moved to first");
                    Log.v(TAG, "Cursor Moved to first and checking");
                    phoneNumber = cursor.getString(phoneNumberIndex);
                }
            } finally {
                Log.v(TAG, "In finally");
                cursor.close();
            }
        }

        Log.v(TAG, "Returning phone number");
        return phoneNumber;
    }
在传递联系人姓名(比如:John Doe)和类型(2是移动类型的int值)时,返回的电话号码为空,即使联系人“John Doe”存在于我的联系人列表中

请帮忙

试试这个

而不是
contacts contract.commonDataTypes.Phone.CONTENT\u URI
Pass
Contacts.Contacts.CONTENT\u URI
查询方法的参数