New contacts created using ContactsContract do not appear in Contacts app

随声附和 提交于 2019-11-27 14:15:03

The sample codes provided by Google work. Just that when it's run on the emulator, no account or group can be found to attach the created contact to. And by default, this newly created contact is not visible.

Using the actual phone (for my case, HTC Dream), after detecting the account name and type to feed in the codes, it works. Alternatively, we can get the visible group ids available and attach the new contact to one of those groups.

To get the available accounts:

//accounts
    Account[] accounts = AccountManager.get(act).getAccounts(); 
    for (Account acc : accounts){
        Log.d(TAG, "account name = " + acc.name + ", type = " + acc.type);
    }

To get the list of groups:

//group membership info
    String[] tempFields = new String[] {
            GroupMembership.GROUP_ROW_ID, GroupMembership.GROUP_SOURCE_ID};
    Cursor tempCur = act.managedQuery(Data.CONTENT_URI, tempFields,
             Data.MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE + "'",
             null, null);

Now, if we want to associate the new contact to a group instead of an account:

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE)
            .withValue(GroupMembership.GROUP_SOURCE_ID, *<THE_IDENTIFIED_GROUP_ID>*)
            .build());

Hope it helps.

user402057

To add an account in emulator that has no groups or accounts, just put "null" as your account or group id, replace the line of code like this

ops.add(ContentProviderOperation
    .newInsert(ContactsContract.RawContacts.CONTENT_URI)
    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)  
    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
    .build());

Did you try to set the visibility of your group to true?

In Contacts Tab press the menu button, than "Display options" > your Account and than check the boxes and "Done".

HTC Sense and MOTOBLUR can be problematic with contacts. I don't know if any of the information here (http://stackoverflow.com/questions/4431101/created-contacts-not-showing-up-on-htc-evo) is useful.

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