使用 ContactsContract 创建的新联系人不会出现在“通讯录"应用中 [英] New contacts created using ContactsContract do not appear in Contacts app

查看:34
本文介绍了使用 ContactsContract 创建的新联系人不会出现在“通讯录"应用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码创建新联系人.它紧跟 Android 提供的 ContactManager 示例.问题是,创建的联系人不会出现在 Android 附带的联系人应用程序中.然而,当我从电话簿加载所有联系人时,我可以看到新创建的联系人.

I'm using the following piece of codes to create a new contact. It follow closely the ContactManager example provided by Android. The problem is, the created contacts do not appear in the Contacts app that shipped with Android. Nevertheless, when I load all the contacts from the phonebook, I can see the newly created contacts.

private void insertPBEntry()抛出 RemoteException、OperationApplicationException{

private void insertPBEntry() throws RemoteException, OperationApplicationException {

     ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

     ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
             .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "Account type")
             .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "Account name")
             .build());

     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, "TOTAL_NEW")
             .build());
     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, "9090")
             .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_MOBILE)
             .build());     
     getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

}

我已经努力搜索,但还没有找到答案.我发现一个答案表明该问题可能与我的字符串帐户类型"和帐户名称"有关(……).就我而言,我不需要创建任何帐户.我想要的只是添加一个带有姓名、电子邮件/邮件地址、电话的新联系人.

I've searched hard but have yet to find the answer. I found one answer suggesting that the problem might have (sth) to do with my strings "Account type" and "Account name". For my case, I do not need to create any account whatsoever. All I want is to add a new contact with a name, email/mail address, phones.

谢谢,伙计们!

推荐答案

Google 提供的示例代码工作.只是当它在模拟器上运行时,找不到帐户或组可以将创建的联系人附加到.默认情况下,这个新创建的联系人是不可见的.

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.

使用实际手机(就我而言,HTC Dream),在检测到帐户名称和输入代码的类型后,它可以工作.或者,我们可以获取可用的可见组 ID,并将新联系人附加到这些组之一.

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.

要获取可用帐户:

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

获取组列表:

//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());

希望有帮助.

这篇关于使用 ContactsContract 创建的新联系人不会出现在“通讯录"应用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆