使用ContactsContract创建新的联系人没有出现在联系人应用程序 [英] New contacts created using ContactsContract do not appear in Contacts app

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

问题描述

我用下面这段codeS创建新的联系人。它密切遵循的Andr​​oid提供的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.

私人无效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.

谢谢你们!

推荐答案

样品codeS由谷歌提供的工作。只是,当它运行在模拟器上,没有任何帐户或组,可以发现附着创建的接触到。而在默认情况下,这个新创建的接触是不可见的。

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梦),检测帐户名称后键入codeS养活,它的工作原理。另外,我们可以得到明显的组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());

希望它帮助。

Hope it helps.

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

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