开发者

Get Postal address from a contact using ContactsContract api on android

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 show开发者_运维技巧ing contacts:

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

I am getting the result in the onActivityResult function.

Please help, how do i do this.


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. 


Can be done this way too (little lesser code)

Send the Intent this way:

Intent intent = new Intent(Intent.ACTION_PICK);

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

In the onActivityResult method

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));
}


Kiran's answer probably works fine for Android 2.0 and above, but when you want it to be compatible for Android 1.6 and below you need to adapt the code a bit:

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

And in onActivtyResult method:

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

Where PICK_CONTACT is a int of your own choice. (Right?)


This will return all the addresses from the contact Postal Address.

     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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜