我正在尝试访问我从联系人选择器意图中选择的人的联系人姓名,电话号码和地址。
这是我用来打开联系人选择器Intent的代码:
这是我正在使用的代码:
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
case PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
System.out.println("in on ActivityResult");
Uri contactData = data.getData();
Cursor c = getActivity().managedQuery(contactData, null,
null, null, null);
if (c.moveToFirst()) {
String id =
c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));StringhasPhone=c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones getActivity().getContentResolver().query(ContactsContract.CommonDataKind s.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
phones.moveToFirst();
String cNumber =
phones.getString(phones.getColumnIndex("data1"));String name phones.getString(phones.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
//here you can find out all the thing.
System.out.println("NAME:"+name);
}Cursor postal_cursor=getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS},
ContactsContract.Data.CONTACT_ID + "=? AND " +
ContactsContract.CommonDataKinds.StructuredPostal.MIMETYPE + "=?",
new String[]{String.valueOf(id),
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE},
null);
postal_cursor.moveToFirst();
while(postal_cursor.moveToNext())
{
String Strt =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String Cty =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String cntry =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String address =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}
postal_cursor.close(); }
但是我无法获取存储的地址。
感谢您的帮助。谢谢
答案 0 :(得分:0)
如果您没有READ_CONTACTS
permission,则只能从Contacts.CONTENT_URI
表中获取信息,而不能从电话或邮政地址等特定信息中获取信息。
如果您想获取电话,则可以使用特殊的电话选择器意图,例如:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
或获取邮政地址:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.StructuredPostal.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
参见此处:https://developer.android.com/guide/components/intents-common#PickContactDat
如果您想同时获得这两条信息,则需要向用户要求READ_CONTACTS
permissions才能使这些查询正常工作。