修改联系方式 [英] Modifying contact information

查看:35
本文介绍了修改联系方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试插入和更新有关现有联系人的信息,因此我创建了一个示例应用程序来开发该功能.我想让我的示例应用程序做的就是插入(或如果存在)更新联系人的电子邮件地址.

I'm trying to insert and update a piece of information on an existing contact so I've created a sample application in order to develop the functionality. All I want my sample app to do is to insert (or if present) update an email address on a contact.

我正在通过系统 Intent 选择联系人,如下所示:

I'm selecting a contact through the system Intent like so:

    startActivityForResult(new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI), PICK_CONTACT_REQUEST);

返回的 URI 是被选中的 Contact (RawContact?) 的 URI,其形式如下:content://com.android.contacts/contacts/lookup/0r2-2A90214945/2.

The URI which is returned is that of the Contact (RawContact?) which was selected and comes in this form: content://com.android.contacts/contacts/lookup/0r2-2A90214945/2.

我可以通过执行以下代码来拉回所有 Data (RawContact?) 项目:

I can pull back all of the Data (RawContact?) items on this by performing the following code:

Cursor cursor = contentResolver.query(mContactUri, null, null, null, null);
try {
    if (cursor.moveToFirst()) {
        for(int i=0; i < cursor.getColumnCount(); i++) {
            String message = cursor.getColumnName(i);
            Log.v("", message);
        }
    }
} finally {
    cursor.close();
}

由此我应该能够确定联系人是否已经拥有 CommonDataTypes.Email 数据 成员:

From this I should be able to determine if the contact already has an CommonDataTypes.Email Data member:

cursor.getColumnIndex(CommonDataKinds.Email.CONTENT_ITEM_TYPE) != -1;

然后执行以下操作之一以InsertUpdate 数据:

And then perform one of the following to either Insert or Update the Data:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation.newInsert(mContactUri)
    .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
    .withValue(Email.DISPLAY_NAME, "somebody@android.com")
    .withValue(Email.TYPE, Email.TYPE_HOME)
    .build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

但这给了我一个例外:java.lang.UnsupportedOperationException:URI:content://com.android.contacts/contacts/lookup/0r2-2A90314945/2,调用用户:

But this gives me an exception: java.lang.UnsupportedOperationException: URI: content://com.android.contacts/contacts/lookup/0r2-2A90314945/2, calling user:

希望有人能看到我错过了什么.

Hopefully someone can see what I've missed.

PS,我正在使用这些权限:

PS, I'm using these permissions:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

推荐答案

Android 用户需要更新他们的 文档.它实际上让我更少了解正在发生的事情,而不是我从猜测中得到的.它建议您可以拉回一个 Contact,其中将包含许多 RawContacts,其中将包含 Data.

The Android people need to update their documentation. It actually served to make me know less about what was happening than I would have gotten from guessing. It suggests that you can pull back a Contact, which will contain many RawContacts which will contain Data.

这种解释是完全错误的.ContactContracts 数据是三个普通的日常数据库表*:

That interpretation is completely wrong. ContactContracts data is instead three normal average everyday database tables*:

ContactContract 表

表格:联系人

访问 URI:Contacts.CONTENT_URI

主键**:Data._ID

说明:

此表包含有关联系人的信息(添加时间、用户图标是什么、是否有自定义铃声).

This table contains information about a Contact (when was it added, what's is it's user icon, does it have a custom ringtone).

关系:它与RawContact 表是一对多的关系.

Relationship: It has a 1-to-many relationship with the RawContact table.

表格:RawContacts

访问 URI:RawContacts.CONTENT_URI

主键:Data._ID

外键**:Data.CONTACT_ID

说明:

此表包含有关一组相关数据项的信息.RawContact 可以包含电子邮件类型、电子邮件显示名称、电话号码、电话显示名称等. RawContact 可以与其他 RawContact 聚合以形成用户看到的 Contact.一个联系人只能包含一个 RawContact.

This table contains information about a related set of Data items. A RawContact could contain Email Type, Email Display Name, Phone Number, Phone Display Name, etc. A RawContact can be aggregated with other RawContacts to make a Contact as a user sees it. A Contact could contain just one RawContact.

关系:它与Data表是一对多的关系.

Relationship: It has a 1-to-many relationship with the Data table.

表格:数据

访问 URI:Data.CONTENT_URI

主键:Data._ID

外键:Data.RAW_CONTACT_ID

说明:

此表包含单个信息字段.电子邮件地址、电话号码、电话号码类型(家庭/工作)、昵称、显示名称.

This table contains a single field of information. An email address, A phone number, A phone number type (home/work), A nickname, A display name.

回答问题

我已将整个示例项目上传到 GitHub,以便其他人了解如何使用 ContactContract 查询、更新和插入记录.

I've uploaded the entire sample project to GitHub in order to allow others to see how to query, update and insert records using ContactContract.

您可以在此处找到要下载的项目:https://github.com/gwoodhouse/ContactContractSample

You can find the project to download here: https://github.com/gwoodhouse/ContactContractSample

如果您只想查看执行查询/更新/插入的 java 代码,这里是类文件:https://github.com/gwoodhouse/ContactContractSample/blob/master/ContactsIntegration/src/com/woodhouse/example/activity/ContactsIntegrationActivity.java

If you just want to look at the java code performing the query/update/insert here is the class file: https://github.com/gwoodhouse/ContactContractSample/blob/master/ContactsIntegration/src/com/woodhouse/example/activity/ContactsIntegrationActivity.java

希望这有帮助!

*不是表格,而是ContentProvider

** 不是严格真的.

这篇关于修改联系方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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