更新 Android 上的联系方式

发布于 2024-08-20 10:19:14 字数 1099 浏览 2 评论 0原文

我希望我的代码能够更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃→可爱长大的 2024-08-27 10:19:14

为了更新联系人中的任何数据,您需要知道该字段的现有 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

绝對不後悔。 2024-08-27 10:19:14

我花了很多时间尝试更新公司和头衔信息。以下现在对我有用。

Uri workUri = Uri.withAppendedPath(contactUri, android.provider.Contacts.Organizations.CONTENT_DIRECTORY);
            values.clear();
            values.put(android.provider.Contacts.Organizations.COMPANY, "company");
            values.put(android.provider.Contacts.Organizations.TYPE, android.provider.Contacts.Organizations.TYPE_WORK);
            values.put(android.provider.Contacts.Organizations.TITLE, "job title");
            values.put(android.provider.Contacts.Organizations.ISPRIMARY, 1);
            getContentResolver().insert(workUri, values); 

I spent a lot of time trying to update the company and title information. The following now works for me.

Uri workUri = Uri.withAppendedPath(contactUri, android.provider.Contacts.Organizations.CONTENT_DIRECTORY);
            values.clear();
            values.put(android.provider.Contacts.Organizations.COMPANY, "company");
            values.put(android.provider.Contacts.Organizations.TYPE, android.provider.Contacts.Organizations.TYPE_WORK);
            values.put(android.provider.Contacts.Organizations.TITLE, "job title");
            values.put(android.provider.Contacts.Organizations.ISPRIMARY, 1);
            getContentResolver().insert(workUri, values); 
在巴黎塔顶看东京樱花 2024-08-27 10:19:14
String[] projection = new String[] { ContactsContract.RawContacts._ID };
        String where = ContactsContract.RawContacts.CONTACT_ID + " = ?";
        String[] selection = new String[] { String.valueOf(1) };
        Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, where, selection, null);
        c.moveToFirst();
        for(int i=0;i<c.getCount();i++)
        {
            Log.e("id",""+c.getString(0));
            c.moveToNext();
        }



        ContentValues values = new ContentValues();
        values.clear();
        values.put(ContactsContract.RawContacts._ID , 0);
        getContentResolver().update(ContactsContract.RawContacts.CONTENT_URI, values,ContactsContract.RawContacts._ID + "+ ?",new String[] { String.valueOf(1) });
String[] projection = new String[] { ContactsContract.RawContacts._ID };
        String where = ContactsContract.RawContacts.CONTACT_ID + " = ?";
        String[] selection = new String[] { String.valueOf(1) };
        Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, where, selection, null);
        c.moveToFirst();
        for(int i=0;i<c.getCount();i++)
        {
            Log.e("id",""+c.getString(0));
            c.moveToNext();
        }



        ContentValues values = new ContentValues();
        values.clear();
        values.put(ContactsContract.RawContacts._ID , 0);
        getContentResolver().update(ContactsContract.RawContacts.CONTENT_URI, values,ContactsContract.RawContacts._ID + "+ ?",new String[] { String.valueOf(1) });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文