修改联系人信息 [英] Modifying contact information

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

问题描述

我试图插入和一个现有的接触,所以我已经为了开发功能创建了一个示例应用程序更新的资料片。我只希望我的示例应用程序做的是插入(或如果present)更新电子邮件地址上的接触。

我选择通过像这样系统意图联系人:

  startActivityForResult(新意图(Intent.ACTION_PICK,Contacts.CONTENT_URI),PICK_CONTACT_REQUEST);
 

这是返回的URI是的联系方式 RawContact ?),它被选中,并配备在这个形成: 内容://com.android.contacts/contacts/lookup/0r2-2A90214945/2

我可以通过执行以下$ C拉回所有的数据 RawContact ?)项目在此$ C:

 光标光标= contentResolver.query(mContactUri,NULL,NULL,NULL,NULL);
尝试 {
    如果(cursor.moveToFirst()){
        的for(int i = 0; I< cursor.getColumnCount();我++){
            字符串消息= cursor.getColumnName(我);
            Log.v(,邮件);
        }
    }
} 最后 {
    cursor.close();
}
 

在此我应该能够确定是否接触已经有一个<一个href="http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Email.html">CommonDataTypes.Email 数据成员:

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

,然后执行以下为插入更新的<一之一href="http://developer.android.com/reference/android/provider/ContactsContract.Data.html">Data:

 的ArrayList&LT; ContentProviderOperation&GT; OPS =新的ArrayList&LT; ContentProviderOperation&GT;();

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)
    。建立());
。getContentResolver()applyBatch(ContactsContract.AUTHORITY,OPS);
 

但是,这给了我一个例外: java.lang.UnsupportedOperationException:网址:内容://com.android.contacts/contacts/lookup/0r2-2A90314945/2,主叫用户:

希望有人可以看到我已经错过了。

PS,我使用这些权限:

 &LT;使用-权限的Andr​​oid:名称=android.permission.READ_CONTACTS/&GT;
&LT;使用-权限的Andr​​oid:名称=android.permission.WRITE_CONTACTS/&GT;
 

解决方案

Android的人需要更新他们的文档。它实际上有助于使我知道的的发生了什么事超出了我的猜测得到。这表明,你可以拉回来联系方式,其中将包含许多 RawContacts 将包含数据

国米pretation是完全错误的。 ContactContracts数据而不是三个正常的平均日常数据库表*:

ContactContract表

表:<一href="http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html">Contacts

  

访问网址: Contacts.CONTENT_URI

     

主键**: Data._ID

     

说明:

     

本表包含联系人(是什么时候加,什么是它的用户图标,它有一个自定义的铃声)的信息。

     

关系:它有一个1对多的 RawContact 表关系

表:<一href="http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html">RawContacts

  

访问网址: RawContacts.CONTENT_URI

     

主键: Data._ID

     

外键**: Data.CONTACT_ID

     

说明:

     

本表包含有关一组相关的数据项的信息。一个RawContact可能包含电子邮件类型,电子邮件显示名称,电话号码,电话显示名称等。RawContact可以聚合与其他RawContacts作联系方式当用户看到它。 A触点可能仅包含一个RawContact。

     

关系:它有一个1对多的数据表关系

表:<一href="http://developer.android.com/reference/android/provider/ContactsContract.Data.html">Data

  

访问网址: Data.CONTENT_URI

     

主键: Data._ID

     

外键: Data.RAW_CONTACT_ID

     

说明:

     

本表中包含的信息的单一领域。电子邮件地址,电话号码,电话号码类型(家庭/工作),昵称,显示名称。

在这个问题的答案

我已经上传了整个样本项目,GitHub上,为了让别人看到了如何使用ContactContract查询,更新和插入记录。

您可以找到项目在这里下载: <一href="https://github.com/gwoodhouse/ContactContractSample">https://github.com/gwoodhouse/ContactContractSample

如果你只是想看看java的code执行查询/更新/插入这里的类文件: <一href="https://github.com/gwoodhouse/ContactContractSample/blob/master/ContactsIntegration/src/com/woodhouse/example/activity/ContactsIntegrationActivity.java">https://github.com/gwoodhouse/ContactContractSample/blob/master/ContactsIntegration/src/com/woodhouse/example/activity/ContactsIntegrationActivity.java

希望这有助于!

<子> *不是表,而是<一个href="http://developer.android.com/reference/android/content/ContentProvider.html">ContentProvider

<子> **没有的严格的真。

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.

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

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

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.

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();
}

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;

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);

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, I'm using these permissions:

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

解决方案

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.

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

ContactContract Tables

Table: Contacts

Access URI: Contacts.CONTENT_URI

Primary Key**: Data._ID

Description:

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

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

Table: RawContacts

Access URI: RawContacts.CONTENT_URI

Primary Key: Data._ID

Foreign Key**: Data.CONTACT_ID

Description:

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.

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

Table: Data

Access URI: Data.CONTENT_URI

Primary Key: Data._ID

Foreign Key: Data.RAW_CONTACT_ID

Description:

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.

In answer to the question

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.

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

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

Hope this helps!

*Not a table, but a ContentProvider

** not strictly true.

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

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