【问题标题】:Contact list only shows one phone number using intent ContactsContract联系人列表仅使用意图 ContactsContract 显示一个电话号码
【发布时间】:2023-03-28 20:41:01
【问题描述】:

我正在尝试从联系人列表中获取电话号码。 我在全球范围内都能做到这一点,但我对拥有多个电话号码的联系人有疑问。

这是我的问题:

  • 在 AVD (API21) 上,联系人列表很好地显示了我联系人的 2 部手机

  • 在我的真实手机(android 5.0.2 => API21)上,它只显示第一部手机。不是全部,因为它在 AVD 上。

你知道为什么 AVD 和我的真实手机的行为不同吗?

我是这样称呼意图的:

    Intent contactIntent = new Intent(Intent.ACTION_PICK,  ContactsContract.Contacts.CONTENT_URI);                           
    contactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(contactIntent, PICK_CONTACT);

这是我的 OnActivity 代码:

public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);


    //Field to fill with the selected phone number
    final EditText textPhone = (EditText) findViewById(R.id.Phone);

    if (data != null) {
        Uri uri = data.getData();

        if (uri != null){

            //Get the phone number id from the Uri
            String id = uri.getLastPathSegment();

            //Query the phone numbers for the selected phone number id
            Cursor c = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone._ID + "=?",
                    new String[]{id}, null);

            int phoneIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            // get the only phone number
            if (c.moveToFirst()) {
                textPhone.setText(c.getString(phoneIdx));
            } else {
                //no result
                Toast noResultFound = Toast.makeText(SendLocationInfoActivity.this, "No phone number found", Toast.LENGTH_SHORT);
                noResultFound.show();
            }
            c.close();
        }
    }
}

【问题讨论】:

  • 请不要作为一种解决方法来显示联系人列表,并在我单击具有多个电话的联系人时显示对话框列表。我试过了,它可以,但是使用这种方法,联系人列表也会显示所有没有电话号码的联系人。
  • 这意味着,联系人显示在 AVD 中,但不在设备中。我说的对吗?
  • 其实AVD显示的是联系人+所有手机,而真机只显示联系人+第一部手机。

标签: android android-contacts phone-number


【解决方案1】:

通过使用 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,用户会看到一个联系人列表,每个电话号码都有一个条目。保证所选联系人有姓名和电话号码。

//Result code for contact picker
private static final int CONTACT_PICKER_RESULT = 1001;                

Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);

如果您想要所有联系人,无论他们是否有电话号码,请将ContactsContract.CommonDataKinds.Phone.CONTENT_URI 替换为ContactsContract.Contacts.CONTENT_URI

这应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 2016-01-08
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多