使用Android SDK> 5,我通过启动ACTION_INSERT活动来创建联系人。 我想使用以下代码为联系人添加几个电话号码(工作,家庭等等):
Intent newIntent = new Intent(Intent.ACTION_INSERT,
ContactsContract.Contacts.CONTENT_URI);
for(ContactInfo.Phone p : phones)
{
newIntent.putExtra(ContactsContract.Intents.Insert.PHONE, p.number);
newIntent.putExtra(ContactsContract.Intents.Insert.PHONE_ISPRIMARY, p.isPrimary ? new Integer(1) : null);
newIntent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, unconvertPhoneType(p.type));
}
(unconvertPhoneType()是一个获取类型为CommonDataKinds.Phone.TYPE_XXX的函数)
我只有一个插入联系人的例子。 上面有什么问题?
此外,在LogCat日志中,我还有以下错误:
12-14 11:09:03.015:WARN / Bundle(1724):键phone_type expected String但值是java.lang.Integer。返回了默认值。
看起来像来自PHONE_TYPE,但CommonDataKinds.Phone.TYPE_XXX是整数类型,所以我不确定...... 造成这种情况的原因是什么?
谢谢!
答案 0 :(得分:0)
logcat告诉你它期待一个String值,而不是一个整数。那是你的问题。电话号码应为String类型。
我从官方文档中提取了此示例,您可以看到电话号码表示为带有“”的字符串。
// Add a phone number for Abraham Lincoln. Begin with the URI for
// the new record just returned by insert(); it ends with the _ID
// of the new record, so we don't have to add the ID ourselves.
// Then append the designation for the phone table to this URI,
// and use the resulting URI to insert the phone number.
phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
values.clear();
values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE);
values.put(People.Phones.NUMBER, "1233214567");
getContentResolver().insert(phoneUri, values);
希望这有帮助。