【发布时间】:2013-12-16 04:43:50
【问题描述】:
我有一个应用程序,允许用户输入电话号码或从他们的联系人中选择一个。为此,我使用:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
//intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT); //PICK_CONTACT is defined earlier as 1
这很好用,但它包括来自各种应用程序的联系人,例如 Facebook 电子邮件等。如果我取消注释
//intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
它仅显示您将在“通讯录”应用中看到的联系人,但不会返回电话号码。有什么办法解决吗?
我确实拥有 READ_CONTACTS 权限。
读取联系人Uri的代码
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
super.onActivityResult(reqCode, resultCode, data);
if(reqCode != PICK_CONTACT) return;
Cursor cur = getContentResolver().query(data.getData(), null,
null, null, null);
if (cur.getCount() <= 0) return;
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// Query phone here. Covered next
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext())
//phone is the EditText view where the user enters a phone number
phone.setText(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
phones.close();
}
}
}
【问题讨论】: