更新 Android 上的联系方式
我希望我的代码能够更新 Android 通讯录中的联系方式(例如姓名、电话号码、电子邮件、组织详细信息等)。我成功修改了一些(具体来说是姓名、电话号码和电子邮件),但不是全部。
每当我尝试更新联系人的组织详细信息(Contacts.Organizations.COMPANY 和 Contacts.Organizations.TITLE)时,我的应用程序都会引发异常,
java.lang.UnsupportedOperationException: Cannot update URL: content://contacts/people/69/organizations/69
代码片段如下:
Uri baseUri = ContentUris.withAppendedId(People.CONTENT_URI, 69);
Uri uri = Uri.withAppendedPath(baseUri, People.Phones.CONTENT_DIRECTORY);
Cursor c = this.getContentResolver().query(uri,
new String[] { Contacts.Organizations._ID, Contacts.Organizations.COMPANY,Contacts.Organizations.TITLE},
null, null, null);
if(c.getCount() > 0) {
uri = ContentUris.withAppendedId(uri, c.getString(0));
ContentValues val1 = new ContentValues();
val1.put(Contacts.Organizations.COMPANY, "arw");
val1.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK);
val1.put(Contacts.Organizations.TITLE, "abcdef");
this.getContentResolver().insert(uri, val1);
I would like my code to update contact details (like name, phone number, email, organization details, etc) in the android contact book. I was successful in modifying a few (name, phone number and email to be specific) but not all.
Whenever I try to update the organization details (Contacts.Organizations.COMPANY and Contacts.Organizations.TITLE) for a contact my app throws an exception
java.lang.UnsupportedOperationException: Cannot update URL: content://contacts/people/69/organizations/69
the code snippet is as follows:
Uri baseUri = ContentUris.withAppendedId(People.CONTENT_URI, 69);
Uri uri = Uri.withAppendedPath(baseUri, People.Phones.CONTENT_DIRECTORY);
Cursor c = this.getContentResolver().query(uri,
new String[] { Contacts.Organizations._ID, Contacts.Organizations.COMPANY,Contacts.Organizations.TITLE},
null, null, null);
if(c.getCount() > 0) {
uri = ContentUris.withAppendedId(uri, c.getString(0));
ContentValues val1 = new ContentValues();
val1.put(Contacts.Organizations.COMPANY, "arw");
val1.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK);
val1.put(Contacts.Organizations.TITLE, "abcdef");
this.getContentResolver().insert(uri, val1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了更新联系人中的任何数据,您需要知道该字段的现有 ID。然后我使用 Builder 类为我想要更新的各个字段获取单独的 ContentProviderOperation 对象,将它们添加到 arrayList 中,然后使用 ContentProvider.applyBatch() 方法
In order to update any data in the contacts you need to know the existing id for that fields. Then I used the Builder class to obtain separate ContentProviderOperation objects for the various fields I wanted to update, add them to an arrayList and then use the ContentProvider.applyBatch() method
我花了很多时间尝试更新公司和头衔信息。以下现在对我有用。
I spent a lot of time trying to update the company and title information. The following now works for me.