【发布时间】: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