从联系人选择器意图获取人员的联系方式

问题描述 投票:0回答:1

我正在尝试访问我从联系人选择器意图中选择的联系人姓名,电话号码和地址。 这是我用来打开联系人选择器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();   }

但是我无法获得存储的地址。

任何帮助表示赞赏。谢谢

android android-intent android-contacts
1个回答
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以使这些查询起作用。

© www.soinside.com 2019 - 2024. All rights reserved.