Problem adding Contact with new API

倾然丶 夕夏残阳落幕 提交于 2019-12-10 10:43:05

问题


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.

    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

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?


回答1:


You need to change the display options to display the contacts associated with the particular account.




回答2:


You probably also need to add it to the "System Group: My Contacts" group.



来源:https://stackoverflow.com/questions/2926298/problem-adding-contact-with-new-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!