使用 Android 上的 ContactsContract api 从联系人获取邮政地址 [英] Get Postal address from a contact using ContactsContract api on android

查看:22
本文介绍了使用 Android 上的 ContactsContract api 从联系人获取邮政地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.

这是我用来显示联系人的代码:

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

<代码>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.

请帮忙,我该怎么做.

推荐答案

在你的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. 

这篇关于使用 Android 上的 ContactsContract api 从联系人获取邮政地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆