Adding a contact usefully with ContactsContract
I'm able to add a Contact with ContactsCo开发者_JAVA百科ntract to one of the user's accounts (a difficult feat in itself!), but I still haven't been able to add it to a group that will show up. I know that the user can go to their contact options and check a "Show All (Other) Contacts" box in their account, but not only is this is an extra step for the user that many of them don't know about, I tried it on an HTC EVO and I'm not sure that it's even possible with Sense.
So at minimum, I'm looking for a ContactsContract equivalent to addToMyContactsGroup. On a broader level, I'm looking for more detail on:
- How to best go about adding the contact to a group, depending on the account they choose to add the contact to.
- How to avoid stacking up multiple Raw Contacts for the same person in case the user presses my "Add to Contacts" button multiple times (all Raw Contacts are visible and separately editable in Android when you edit a contact).
- Basically how to make adding a contact as user friendly as possible without using
Intents.Insert
Thanks pros!
I'm not sure about the groups, but this is what I used for the rest:
(READ TO BOTTOM FOR EXPLANATION)
Declarations:
static ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Action code:
ops.addAll(fillContentProviderOperation(accounts, ctaList,
ops));
private ArrayList<ContentProviderOperation> fillContentProviderOperation(
Account[] accounts, ArrayList<ContactToAdd> ctaList,
ArrayList<ContentProviderOperation> privateOps) //
{
for (int i = 0; i < ctaList.size(); i++) //
{
if (ctaList.get(i) != null) //
{
if (ctaList.get(i).LastName != ""
&& ctaList.get(i).LastName != null) //
{
privateOps.addAll(addToContacts(ctaList.get(i),
privateOps.size(), accounts,
ctaList.get(i).groupType));
publishProgress();
}
}
}
return privateOps;
}
Filler code:
protected ArrayList<ContentProviderOperation> addToContacts(
ContactToAdd cta, int opsLength, Account[] accounts, String groupName) //
{
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, accounts[0].type)
.withValue(RawContacts.ACCOUNT_NAME, accounts[0].name).build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.GIVEN_NAME, cta.FirstName)
.withValue(StructuredName.FAMILY_NAME, cta.LastName).build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, cta.DayWorkPhoneNumber.PhoneNumber)
.withValue(Phone.TYPE, Phone.TYPE_MOBILE).build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.DATA1, cta.Email)
.withValue(Email.TYPE, Email.TYPE_MOBILE).build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(StructuredPostal.STREET, cta.MailingAddress.Address1)
.withValue(StructuredPostal.CITY, cta.MailingAddress.City)
.withValue(StructuredPostal.REGION,
cta.MailingAddress.StateCode)
.withValue(StructuredPostal.POSTCODE,
cta.MailingAddress.PostalCode)
.withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME)
.build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, opsLength)
.withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(StructuredPostal.STREET,
cta.ShippingAddress.Address1)
.withValue(StructuredPostal.CITY, cta.ShippingAddress.City)
.withValue(StructuredPostal.REGION,
cta.ShippingAddress.StateCode)
.withValue(StructuredPostal.POSTCODE,
cta.ShippingAddress.PostalCode)
.withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK)
.build());
return ops;
}
This code basically fills a large ArrayList ops with several values to insert into the contacts database.
You need to make sure you use the .withValueBackReference(opsLength) so you are pointing BACK at the correct raw contact.
This code is tested and works on an HTC Incredible running 2.2.
Good luck!
精彩评论