Android 使用 ContactsContract 创建的新联系人不会出现在“通讯录”应用中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3336019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New contacts created using ContactsContract do not appear in Contacts app
提问by Paul Hoang
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.
我正在使用以下代码创建新联系人。它紧跟 Android 提供的 ContactManager 示例。问题是,创建的联系人不会出现在 Android 附带的联系人应用程序中。然而,当我从电话簿加载所有联系人时,我可以看到新创建的联系人。
private void insertPBEntry() throws RemoteException, OperationApplicationException {
private void insertPBEntry() 抛出 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.
我已经努力搜索,但还没有找到答案。我发现一个答案表明该问题可能与我的字符串“帐户类型”和“帐户名称”有关(……)。就我而言,我不需要创建任何帐户。我想要的只是添加一个带有姓名、电子邮件/邮件地址、电话的新联系人。
Thanks, guys!
谢谢你们!
回答by Paul Hoang
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.
Google 提供的示例代码有效。只是当它在模拟器上运行时,找不到帐户或组可以将创建的联系人附加到。默认情况下,这个新创建的联系人是不可见的。
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.
使用实际的手机(就我而言,HTC Dream),在检测到帐户名称和输入代码的类型后,它就可以工作了。或者,我们可以获取可用的可见组 ID,并将新联系人附加到这些组之一。
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.
希望能帮助到你。
回答by 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
要在没有组或帐户的模拟器中添加帐户,只需将“null”作为您的帐户或组 ID,替换如下代码行
ops.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
回答by white_gecko
Did you try to set the visibility of your group to true?
您是否尝试将组的可见性设置为 true?
In Contacts Tab press the menu button, than "Display options" > your Account and than check the boxes and "Done".
在“联系人”选项卡中,按菜单按钮,然后是“显示选项”> 您的帐户,然后选中复选框和“完成”。
回答by Jon O
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.
HTC Sense 和 MOTOBLUR 的触点可能会出现问题。我不知道这里的任何信息(http://stackoverflow.com/questions/4431101/created-contacts-not-showing-up-on-htc-evo)是否有用。