从ID为Android的联系人提供商处检索电话号码

时间:2017-04-29 02:41:06

标签: android

我可以检索联系人ID,但稍后我希望根据联系人ID单独检索电话号码。下面的代码返回电话号码的空结果。 (我希望以后能够一起检索姓名和电话号码并填写视图,但我只是想让电话号码先工作)。

在我的onCreate中我有这段代码

   String phoneNum = getPhoneNumber(myID);
   TextView phoneTextView = (TextView) findViewById(R.id.textViewPhone);
   phoneTextView.setText(phoneNum);

这是getPhoneNumber()

的方法
    protected String getPhoneNumber(String id) {
    ArrayList<String> phones = new ArrayList<String>();

    Cursor cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
            new String[]{id}, null);

    while (cursor.moveToNext()) {
        phones.add(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
    }

    cursor.close();

    String phoneNum;

    phoneNum = phones.get(0);
    return phoneNum;
}//end getPhoneNumber();

}

这会产生错误java.lang.IndexOutOfBoundsException:索引0无效,大小为0,我计划为其创建一些错误处理。但是,我仍然确定我有前面代码中的ID,所以我不知道为什么ArrayList返回null。如果您想查看该代码,它也在我的onCreate中:

    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            if (cursor.getCount() != 0) {
                int numContacts = cursor.getCount();

                ArrayList<String> idList = new ArrayList<>();
                Random rand = new Random();
                int randomNum = rand.nextInt(numContacts);

                while (cursor.moveToNext()) {

                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    idList.add(id);
                }

                myID = idList.get(randomNum);
                String myString = Integer.toString(randomNum);
                TextView myTextView = (TextView) findViewById(R.id.textViewID);
                myTextView.setText(myString);
                if (myID != null) {

                    myTextView.setText(myID);
                } else {

                    myTextView.setText("Try Again!");
                }
            } else {
                Toast.makeText(getApplicationContext(), "Your have no contacts.", Toast.LENGTH_SHORT).show();
            }
            cursor.close();

2 个答案:

答案 0 :(得分:1)

// You can fetch the Contact Number and Email With Following Methods.
String phone = getPhoneNumber(ContactId);
String email = getEmail("" + ContactId);

private String getPhoneNumber(long id) {
        String phone = null;
        Cursor phonesCursor = null;
        phonesCursor = queryPhoneNumbers(id);
        if (phonesCursor == null || phonesCursor.getCount() == 0) {
            // No valid number
            //signalError();
            return null;
        } else if (phonesCursor.getCount() == 1) {
            // only one number, call it.
            phone = phonesCursor.getString(phonesCursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        } else {
            phonesCursor.moveToPosition(-1);
            while (phonesCursor.moveToNext()) {

                // Found super primary, call it.
                phone = phonesCursor.getString(phonesCursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                break;

            }
        }

        return phone;
    }

    private Cursor queryPhoneNumbers(long contactId) {
        ContentResolver cr = getContentResolver();
        Uri baseUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                contactId);
        Uri dataUri = Uri.withAppendedPath(baseUri,
                ContactsContract.Contacts.Data.CONTENT_DIRECTORY);

        Cursor c = cr.query(dataUri, new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.NUMBER,
                        ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY, ContactsContract.RawContacts.ACCOUNT_TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.LABEL},
                ContactsContract.Data.MIMETYPE + "=?",
                new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}, null);
        if (c != null && c.moveToFirst()) {
            return c;
        }
        return null;
    }


    private String getEmail(String id) {
        String email = "";
        ContentResolver cr = getContentResolver();
        Cursor emailCur = cr.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                new String[]{id}, null);
        while (emailCur.moveToNext()) {
            // This would allow you get several email addresses
            // if the email addresses were stored in an array
            email = emailCur.getString(
                    emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//          String emailType = emailCur.getString(
//                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
        }
        emailCur.close();
        return email;
    }

答案 1 :(得分:0)

我无法根据联系人ID成功检索代码 - 但这篇帖子给了我一个线索:Retrieving a phone number with ContactsContract in Android - function doesn't work

我已修改原始代码以在DISPLAY_NAME上进行查询,现在情况正常。这是我用来检索电话号码的方法:

     private String retrieveContactNumber(String contactName) {

    Log.d(TAG, "Contact Name: " + contactName);

    Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ? AND " +
                    ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

            new String[]{contactName},
            null);

    if (cursorPhone.moveToFirst()) {
        contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }

    cursorPhone.close();

    Log.d(TAG, "Contact Phone Number: " + contactNumber);

    return contactNumber;
}