获取邮政地址使用ContactsContract API android上的接触邮政、地址、ContactsContract、API

2023-09-04 08:13:30 作者:泪刺痛了眼

我使用ContactsContract API意图表示从活动的所有联系人。这个意图返回接触的ID。我需要得到这个联系人的邮寄地址。

I am using ContactsContract api intent for showing all contacts from an activity. This intent returns an id of the contact. I need to get the postal address of this contact.

下面是我使用的显示联系人的code:

Here is the code which i am using for showing contacts:

意向意图=新的意图(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); startActivityForResult(意向,PICK_CONTACT);

Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); startActivityForResult(intent, PICK_CONTACT);

我得到的结果在 onActivityResult 的功能。

I am getting the result in the onActivityResult function.

请帮忙,我如何做到这一点。

Please help, how do i do this.

推荐答案

在你onActivityResult,首先要使接触式ID和查询ContactsContract.Data的相关数据:

in your onActivityResult, first get the contact ID and query the ContactsContract.Data for the relevant data:

// get the contact ID

Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
cursor.moveToFirst();
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
cursor.close();

// get the data package containg the postal information for the contact
cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, 
    new String[]{ StructuredPostal.STREET,
        StructuredPostal.CITY,
// add more coluns from StructuredPostal if you need them
        StructuredPostal.POSTCODE},
        ContactsContract.Data.CONTACT_ID + "=? AND " +
            StructuredPostal.MIMETYPE + "=?",
        new String[]{String.valueOf(id), StructuredPostal.CONTENT_ITEM_TYPE},
        null);


Street = cursor.getString(cursor.getColumnIndex(StructuredPostal.STREET));
Postcode = cursor.getString(cursor.getColumnIndex(StructuredPostal.POSTCODE));
City = cursor.getString(cursor.getColumnIndex(StructuredPostal.CITY)));
// etc.