问题描述
我要建一个访问用户的个人资料在Android接触的应用程序。不过,也有一些情况下,用户可能没有资料接触成立。在这种情况下,我想以编程方式插入的轮廓接触。然而,我无法做到这一点。
I'm building an app that accesses a users 'Profile' contact on Android. However, there are some cases where the user may not have a 'Profile' contact set up. In this case, I would like to programatically insert the profile contact. However, I am unable to achieve this.
下面是我的code迄今:
Here is my code so far:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Profile.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,null )
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "9X-XXXXXXXXX")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Mike Sullivan")
.build());
try {
ContentProviderResult[] res = getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但我得到一个异常:
But I get an exception:
java.lang.UnsupportedOperationException: The profile contact is created automatically
于是,我就插入改为像这样的更新:
So I tried to change the insert to an update like so:
ContentProviderOperation.newUpdate(ContactsContract.Profile.CONTENT_URI)
但尽管如此,好像没有什么改变,并没有插入制成。我甚至尝试更新code的,而的我有一个现有的配置文件的接触,但它仍然无法正常工作。
But still, nothing seems to happen, and no insertions are made. I even tried the update code while I had an existing profile contact, but it still does not work.
然而,文件说,插入和updations应该是可能的:
However, the documentation says that insertions and updations should be possible:
的
我是什么没有看到吗?
推荐答案
我终于想通了。我不得不插入原始接触到的个人资料的具体原料接触表使用ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI。下面是完整的code:
I finally figured it out. I had to insert a raw contact into the 'profile' specific raw contacts table using ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI. Here is the full code:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,null )
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());
try {
ContentProviderResult[] res = getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
希望这可以帮助别人!
Hope this helps someone!
这篇关于如何在Android的编程方式插入“个人资料”的接触?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!