How to add postal address to contacts in android p

2019-03-05 22:51发布

问题:

I am developing app which add contact info to android contact list .to How to add postal address to contacts in android programmatically ?

回答1:

Postal address are stored like all the other info in the DATA table with a

MIMEtype == ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE

Please google ContactsContract.CommonDataKinds.StructuredPostalto find all the info.

If you need to know how to edit a contact in general I would suggest you to have a look to the SampleSyncAdapter in the Android SDK. It is a sync adapter so you don't need to study everything but updateContact in ContactManager is a good point to start with.



回答2:

It's been a while that this has been asked but maybe someone else is still interested in it. How to add a contact with address info:

import static android.provider.ContactsContract.Data;
import static android.provider.ContactsContract.Intents.Insert;

private void createContactIntent() {
    Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION, ContactsContract.Contacts.CONTENT_URI);
    contactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    contactIntent.putExtra(Insert.NAME, "Sergio Mendes");
    contactIntent.putExtra(Insert.COMPANY, "Company One");
    contactIntent.putExtra(Insert.POSTAL, "Street 1, 9999 City, Country");
    contactIntent.putExtra(Data.IS_SUPER_PRIMARY, 1);
    startActivity(contactIntent);
}

Note that some devices like Samsung S5 / A5 will put the whole address into the "street" field. If you have any optimizations for this let me know.