我可以将一个与ContactsContract的联系人添加到用户的一个帐户中(这本身就是一项艰巨的任务!),但我仍然无法将其添加到将要显示的组中。我知道用户可以转到他们的联系方式并查看他们帐户中的“显示所有(其他)联系人”框,但这不仅仅是用户的一个额外步骤,他们中的许多人都不知道,我在HTC EVO上尝试过,我不确定Sense是否可行。
至少,我正在寻找与addToMyContactsGroup相当的ContactsContract。在更广泛的层面上,我正在寻找更多细节:
Intents.Insert
非常感谢!
答案 0 :(得分:1)
我不确定这些群体,但这是我用来做其余的事情:
(阅读底部解释)
声明:
static ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
行动代码:
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;
}
填充码:
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;
}
此代码基本上填充了一个大型ArrayList操作,其中有几个值要插入到contacts数据库中。
您需要确保使用.withValueBackReference(opsLength),以便指向正确的原始联系人。
此代码经过测试,适用于运行2.2的HTC Incredible。
祝你好运!