I am trying to add a new contact to my contact list using the new ContactContract API via my application. I have the following method based on the Contact Manager example on android dev.
我正在尝试通过我的应用程序使用新的ContactContract API将新联系人添加到我的联系人列表中。我有基于android dev上的Contact Manager示例的以下方法。
private static void addContactCore(Context context, String accountType, String accountName, String name, String phoneNumber, int phoneType) throws RemoteException, OperationApplicationException {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
//Add contact type
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
.build());
//Add contact name
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, (!name.toLowerCase().equals("unavailable") && !name.equals("")) ? name : phoneNumber)
.build());
//Add phone number
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phoneType)
.build());
//Add contact
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
In one example I have the flowing values for the parameters. accountType:com.google accountName:(my google account email) name:Mike phoneNumber:5555555555 phoneType:3
在一个示例中,我具有参数的流动值。 accountType:com.google accountName :(我的Google帐户电子邮件)名称:Mike phoneNumber:5555555555 phoneType:3
The call to the function returns normally without any exception being thrown however the contact is no where to be found in the contact manager on my phone. There is also no contact with that information on my phone already. Does anyone have any insight into what I might be doing wrong?
对函数的调用通常会返回而不会抛出任何异常,但是联系人无法在我的手机上的联系人管理器中找到。我的手机上也没有接触过这些信息。有没有人对我可能做错了什么有任何见解?
2 个解决方案
#1
1
You need to change the display options to display the contacts associated with the particular account.
您需要更改显示选项以显示与特定帐户关联的联系人。
#2
0
You probably also need to add it to the "System Group: My Contacts" group.
您可能还需要将其添加到“系统组:我的联系人”组。
#1
1
You need to change the display options to display the contacts associated with the particular account.
您需要更改显示选项以显示与特定帐户关联的联系人。
#2
0
You probably also need to add it to the "System Group: My Contacts" group.
您可能还需要将其添加到“系统组:我的联系人”组。