我想创建一个应用程序,向Android中的联系人添加个人详细信息。我可以使用以下代码添加姓名,电话号码等:
intent addContactIntent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
addContactIntent.putExtra(Contacts.Intents.Insert.NAME, "myname");
addContactIntent.putExtra(Contacts.Intents.Insert.PHONE, "232323222");
addContactIntent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, Contacts.Phones.TYPE_MOBILE);
但问题是在地址栏中有城市,街道等,而且我在这里无法添加数据。
答案 0 :(得分:1)
可能有点晚了,但希望对于遇到同样问题的人仍然有帮助:
即使Contacts.Intents.Inserts已弃用,ContactsContract.Intents.Insert API仍然无法通过Intent添加城市,街道等。要解决此问题,您必须使用ContactsContract API以编程方式执行此操作。这是我的解决方案:
首先我回复一些“addContactEvent” - 在我的例子中点击一个按钮 - 用Toast。我的addContactToAddressBook方法返回String Ressource的Id,它描述了操作的结果。
int resultId = addContactToAddressBook(myContactObject, this);
Toast.makeText(this, getString(resultId), Toast.LENGTH_SHORT).show();
接下来,我将大量信息添加到联系地址簿中,例如街道,城市,名称......信息存储在联系人对象中。
public int addContactToAddressBook(MyContactObject contact, Context context){
// is contact already in contacts??
if(contact != null && !numberAlreadyInContacts(contact.getPhoneNumber(), context)){
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
// prepare Contact
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.build());
// Insert First- and LastName
if(contact.getFirstName() != null && contact.getLastName() != null){
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, "Lost Symbol Characters")
.withValue(StructuredName.GIVEN_NAME, contact.getFirstName())
.withValue(StructuredName.FAMILY_NAME, contact.getLastName())
.build());
}
// Insert Organistion and JobTitle
if(contact.getOrganisation() != null && contact.getJobTitle() != null){
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE)
.withValue(Organization.COMPANY, contact.getOrganisation())
.withValue(Organization.TYPE, Organization.TYPE_WORK)
.withValue(Organization.TITLE, contact.getJobTitle())
.withValue(Organization.TYPE, Organization.TYPE_WORK)
.build());
}
// Insert Email
if(contact.getEmailAddress() != null){
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.DATA, contact.getEmailAddress())
.withValue(Email.TYPE, Email.TYPE_WORK)
.build());
}
// Insert PhoneNumber
if(contact.getPhoneNumber() != null){
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, contact.getPhoneNumber())
.withValue(Phone.TYPE, Phone.TYPE_WORK)
.build());
}
// Insert MobileNumber
if(contact.getMobileNumber() != null){
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, contact.getMobileNumber())
.withValue(Phone.TYPE, Phone.TYPE_MOBILE)
.build());
}
// Insert Address
if(contact.getStreetAddress() != null && contact.getPostalCode() != null && contact.getLocation() != null && contact.getCountry() != null){
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(StructuredPostal.COUNTRY, contact.getCountry())
.withValue(StructuredPostal.POSTCODE, contact.getPostalCode())
.withValue(StructuredPostal.CITY, contact.getLocation())
.withValue(StructuredPostal.STREET, contact.getStreetAddress())
.build());
}
// add to DB
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
return R.string.contact_added;
} catch (RemoteException e) {
return R.string.adding_contact_failed;
} catch (OperationApplicationException e) {
return R.string.adding_contact_failed;
}
} else if(contact != null && numberAlreadyInContacts(contact.getPhoneNumber(), context)){
return R.string.contact_exists;
} else {
return R.string.adding_contact_failed;
}
}
在这种方法中,我试着找出联系人是否已经在我的地址簿中。因此,我会查找电话号码,因为它应该对所有联系人都是唯一的。
private boolean numberAlreadyInContacts(String number, Context context) {
if(number == null && number.length()<1){
return false;
}
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
ContentResolver contentResolver = context.getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.close();
return true;
}
contactLookup.close();
return false;
}
享受此代码的乐趣!
克里斯