本课程向您展示如何使用 Intent
插入新联系人或修改联系人的数据。不同于直接访问联系人提供方,Intent
会启动联系人应用,该应用会运行相应的 Activity
。对于本课程中介绍的修改操作,如果您在 Intent
中发送扩展数据,这些数据会输入到启动的 Activity
的界面中。
使用 Intent
插入或更新单个联系人是修改联系人提供方的首选方法,原因如下
- 它节省了您开发自己的界面和代码的时间和精力。
- 它避免了因不遵循联系人提供方规则的修改而引入错误。
- 它减少了您需要请求的权限数量。您的应用不需要写入联系人提供方的权限,因为它将修改委托给了联系人应用,而联系人应用已经拥有该权限。
使用 intent 插入新联系人
当您的应用接收到新数据时,您通常希望允许用户插入新联系人。例如,一家餐厅评论应用可以允许用户在评论餐厅时将其添加为联系人。要使用 intent 执行此操作,请使用尽可能多的可用数据创建 intent,然后将 intent 发送到联系人应用。
使用联系人应用插入联系人会将新的 raw(原始)联系人插入到联系人提供方的 ContactsContract.RawContacts
表中。如有必要,联系人应用会在创建原始联系人时提示用户输入要使用的帐号类型和帐号。联系人应用还会通知用户该原始联系人是否已存在。用户可以选择取消插入,在这种情况下不会创建联系人。要详细了解原始联系人,请参阅联系人提供方 API 指南。
创建 intent
首先,创建一个新的 Intent
对象,操作为 Intents.Insert.ACTION
。将 MIME 类型设置为 RawContacts.CONTENT_TYPE
。例如:
Kotlin
... // Creates a new Intent to insert a contact val intent = Intent(ContactsContract.Intents.Insert.ACTION).apply { // Sets the MIME type to match the Contacts Provider type = ContactsContract.RawContacts.CONTENT_TYPE }
Java
... // Creates a new Intent to insert a contact Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION); // Sets the MIME type to match the Contacts Provider intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
如果您已经有联系人的详细信息,例如电话号码或电子邮件地址,您可以将它们作为扩展数据插入到 intent 中。对于键值,请使用 Intents.Insert
中的相应常量。联系人应用会在其插入屏幕中显示这些数据,以便用户进行进一步的编辑和添加。
Kotlin
private var emailAddress: EditText? = null private var phoneNumber: EditText? = null ... /* Assumes EditText fields in your UI contain an email address * and a phone number. * */ emailAddress = findViewById(R.id.email) phoneNumber = findViewById(R.id.phone) ... /* * Inserts new data into the Intent. This data is passed to the * contacts app's Insert screen */ intent.apply { // Inserts an email address putExtra(ContactsContract.Intents.Insert.EMAIL, emailAddress?.text) /* * In this example, sets the email type to be a work email. * You can set other email types as necessary. */ putExtra( ContactsContract.Intents.Insert.EMAIL_TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK ) // Inserts a phone number putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber?.text) /* * In this example, sets the phone type to be a work phone. * You can set other phone types as necessary. */ putExtra( ContactsContract.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK ) }
Java
private EditText emailAddress = null; private EditText phoneNumber = null; ... /* Assumes EditText fields in your UI contain an email address * and a phone number. * */ emailAddress = (EditText) findViewById(R.id.email); phoneNumber = (EditText) findViewById(R.id.phone); ... /* * Inserts new data into the Intent. This data is passed to the * contacts app's Insert screen */ // Inserts an email address intent.putExtra(ContactsContract.Intents.Insert.EMAIL, emailAddress.getText()) /* * In this example, sets the email type to be a work email. * You can set other email types as necessary. */ .putExtra(ContactsContract.Intents.Insert.EMAIL_TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK) // Inserts a phone number .putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber.getText()) /* * In this example, sets the phone type to be a work phone. * You can set other phone types as necessary. */ .putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
创建 Intent
后,通过调用 startActivity()
发送它。
Kotlin
/* Sends the Intent */ startActivity(intent)
Java
/* Sends the Intent */ startActivity(intent);
此调用会在联系人应用中打开一个屏幕,允许用户输入新联系人。联系人的帐号类型和帐号名称显示在屏幕顶部。用户输入数据并点击 Done(完成)后,联系人应用的联系人列表将出现。用户可以通过点击 Back(返回)返回到您的应用。
使用 intent 编辑现有联系人
如果用户已选择了感兴趣的联系人,使用 Intent
编辑现有联系人会很有用。例如,一个查找拥有邮政地址但缺少邮政编码的联系人的应用,可以为用户提供查找编码然后将其添加到联系人的选项。
要使用 intent 编辑现有联系人,请使用与插入联系人类似的流程。按照 使用 intent 插入新联系人 部分的描述创建 intent,但要将联系人的 Contacts.CONTENT_LOOKUP_URI
和 MIME 类型 Contacts.CONTENT_ITEM_TYPE
添加到 intent 中。如果您想用已有的详细信息编辑联系人,可以将其放入 intent 的扩展数据中。请注意,某些姓名列无法使用 intent 进行编辑;这些列列在类 ContactsContract.Contacts
的 API 参考文档的摘要部分,标题为“更新”下。
最后,发送 intent。联系人应用会响应并显示一个编辑屏幕。当用户完成编辑并保存后,联系人应用会显示联系人列表。当用户点击 Back(返回)时,会显示您的应用。
创建 intent
要编辑联系人,请调用 Intent(action)
创建操作为 ACTION_EDIT
的 intent。调用 setDataAndType()
将 intent 的数据值设置为联系人的 Contacts.CONTENT_LOOKUP_URI
,并将 MIME 类型设置为 Contacts.CONTENT_ITEM_TYPE
MIME 类型;因为调用 setType()
会覆盖 Intent
的当前数据值,所以您必须同时设置数据和 MIME 类型。
要获取联系人的 Contacts.CONTENT_LOOKUP_URI
,请调用 Contacts.getLookupUri(id, lookupkey)
,并将联系人的 Contacts._ID
和 Contacts.LOOKUP_KEY
值作为参数。
注意: 联系人的 LOOKUP_KEY
值是您应该用于检索联系人的标识符。它保持不变,即使提供方更改联系人的行 ID 以处理内部操作。
以下代码段展示了如何创建 intent
Kotlin
// The Cursor that contains the Contact row var mCursor: Cursor? = null // The index of the lookup key column in the cursor var lookupKeyIndex: Int = 0 // The index of the contact's _ID value var idIndex: Int = 0 // The lookup key from the Cursor var currentLookupKey: String? = null // The _ID value from the Cursor var currentId: Long = 0 // A content URI pointing to the contact var selectedContactUri: Uri? = null ... /* * Once the user has selected a contact to edit, * this gets the contact's lookup key and _ID values from the * cursor and creates the necessary URI. */ mCursor?.apply { // Gets the lookup key column index lookupKeyIndex = getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY) // Gets the lookup key value currentLookupKey = getString(lookupKeyIndex) // Gets the _ID column index idIndex = getColumnIndex(ContactsContract.Contacts._ID) currentId = getLong(idIndex) selectedContactUri = ContactsContract.Contacts.getLookupUri(currentId, mCurrentLookupKey) } // Creates a new Intent to edit a contact val editIntent = Intent(Intent.ACTION_EDIT).apply { /* * Sets the contact URI to edit, and the data type that the * Intent must match */ setDataAndType(selectedContactUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE) }
Java
// The Cursor that contains the Contact row public Cursor mCursor; // The index of the lookup key column in the cursor public int lookupKeyIndex; // The index of the contact's _ID value public int idIndex; // The lookup key from the Cursor public String currentLookupKey; // The _ID value from the Cursor public long currentId; // A content URI pointing to the contact Uri selectedContactUri; ... /* * Once the user has selected a contact to edit, * this gets the contact's lookup key and _ID values from the * cursor and creates the necessary URI. */ // Gets the lookup key column index lookupKeyIndex = mCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY); // Gets the lookup key value currentLookupKey = mCursor.getString(lookupKeyIndex); // Gets the _ID column index idIndex = mCursor.getColumnIndex(ContactsContract.Contacts._ID); currentId = mCursor.getLong(idIndex); selectedContactUri = Contacts.getLookupUri(currentId, mCurrentLookupKey); ... // Creates a new Intent to edit a contact Intent editIntent = new Intent(Intent.ACTION_EDIT); /* * Sets the contact URI to edit, and the data type that the * Intent must match */ editIntent.setDataAndType(selectedContactUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
添加导航标记
在 Android 4.0 (API 版本 14) 及更高版本中,联系人应用中的一个问题会导致导航不正确。当您的应用向联系人应用发送编辑 intent,并且用户编辑并保存联系人后,当他们点击 Back(返回)时,会看到联系人列表屏幕。要导航回您的应用,他们必须点击 Recents(最近使用的应用)并选择您的应用。
为了解决 Android 4.0.3 (API 版本 15) 及更高版本中的这个问题,请向 intent 添加扩展数据键 finishActivityOnSaveCompleted
,其值为 true
。Android 4.0 之前的版本接受此键,但无效。要设置扩展数据,请执行以下操作
Kotlin
// Sets the special extended data for navigation editIntent.putExtra("finishActivityOnSaveCompleted", true)
Java
// Sets the special extended data for navigation editIntent.putExtra("finishActivityOnSaveCompleted", true);
添加其他扩展数据
要向 Intent
添加其他扩展数据,请根据需要调用 putExtra()
。您可以使用 Intents.Insert
中指定的键值添加常见联系人字段的扩展数据。请记住,ContactsContract.Contacts
表中的某些列无法修改。这些列列在类 ContactsContract.Contacts
的 API 参考文档的摘要部分,标题为“更新”下。
发送 intent
最后,发送您已构建的 intent。例如:
Kotlin
// Sends the Intent startActivity(editIntent)
Java
// Sends the Intent startActivity(editIntent);
让用户选择使用 intent 插入或编辑
您可以通过发送操作为 Intent
的 ACTION_INSERT_OR_EDIT
让用户选择是插入联系人还是编辑现有联系人。例如,电子邮件客户端应用可以允许用户将收到的电子邮件地址添加到新联系人,或作为现有联系人的附加地址。将此 intent 的 MIME 类型设置为 Contacts.CONTENT_ITEM_TYPE
,但不要设置数据 URI。
发送此 intent 后,联系人应用会显示联系人列表。用户可以插入新联系人,也可以选择现有联系人并进行编辑。您添加到 intent 中的任何扩展数据字段都会填充出现的屏幕。您可以使用 Intents.Insert
中指定的任何键值。以下代码段展示了如何构建和发送 intent
Kotlin
// Creates a new Intent to insert or edit a contact val intentInsertEdit = Intent(Intent.ACTION_INSERT_OR_EDIT).apply { // Sets the MIME type type = ContactsContract.Contacts.CONTENT_ITEM_TYPE } // Add code here to insert extended data, if desired // Sends the Intent with an request ID startActivity(intentInsertEdit)
Java
// Creates a new Intent to insert or edit a contact Intent intentInsertEdit = new Intent(Intent.ACTION_INSERT_OR_EDIT); // Sets the MIME type intentInsertEdit.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); // Add code here to insert extended data, if desired ... // Sends the Intent with an request ID startActivity(intentInsertEdit);