在android上使用ContactsContract api从联系人处获取邮政地址

时间:2010-08-31 13:57:19

标签: android sdk contacts contactscontract

我正在使用ContactsContract api intent来显示活动中的所有联系人。此意图返回联系人的ID。我需要获得此联系人的邮政地址。

以下是我用于显示联系人的代码:

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

我在onActivityResult函数中得到了结果。

请帮忙,我该怎么做。

4 个答案:

答案 0 :(得分:10)

在你的onActivityResult中,首先获取联系人ID并查询ContactsContract.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. 

答案 1 :(得分:2)

也可以这样做(代码较小)

以这种方式发送意图:

Intent intent = new Intent(Intent.ACTION_PICK);

intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_TYPE); startActivityForResult(intent, PICK_CONTACT);

在onActivityResult方法

Uri contactData = data.getData();
Cursor c =  managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
    String address = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}

答案 2 :(得分:1)

Kiran的答案可能适用于Android 2.0及更高版本,但是如果你希望它兼容Android 1.6及以下版本,你需要稍微修改一下代码:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(Contacts.ContactMethods.CONTENT_POSTAL_TYPE);
startActivityForResult(intent, PICK_CONTACT);

在onActivtyResult方法中:

Uri contactData = data.getData();
Cursor c =  managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
   String address = c.getString(c.getColumnIndexOrThrow(Contacts.ContactMethodsColumns.DATA));
}

PICK_CONTACT是您自己选择的int。 (右?)

答案 3 :(得分:1)

这将返回联系邮政地址的所有地址。

     Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
     String sortOrder = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
      Cursor cr = getContentResolver().query(uri,null, null, null, sortOrder);
      while(cr.moveToNext())
{
    String  Street = cr.getString(cr.getColumnIndex(StructuredPostal.STREET));
    String  Postcode = cr.getString(cr.getColumnIndex(StructuredPostal.POSTCODE));
    String  City = cr.getString(cr.getColumnIndex(StructuredPostal.CITY));
    Toast.makeText(this,"Address is : " +City+","+Street ,Toast.LENGTH_SHORT).show();
      }
      cr.close();