本课将介绍如何使用 Intent 插入新联系人或修改联系人的数据。Intent 不会直接访问联系人提供程序,而是会启动通讯录应用,该应用会运行相应的 Activity。对于本课中介绍的修改操作,如果您在 Intent 中发送扩展数据,这些数据会输入到已启动的 Activity 的界面中。
使用 Intent 插入或更新单个联系人是修改联系人提供程序的首选方式,原因如下:
- 您无需开发自己的界面和代码,节省了时间和精力。
- 这样可以避免因不遵循联系人提供程序规则的修改而引入错误。
- 它减少了您需要请求的权限数量。您的应用无需具有向联系人提供程序写入数据的权限,因为它会将修改操作委托给已具有该权限的通讯录应用。
使用 intent 插入新联系人
您一般希望允许用户在您的应用收到新数据时插入新联系人。例如,餐厅评价应用可以允许用户在评价餐厅时将其添加为联系人。如需使用 intent 执行此操作,请使用尽可能多的可用数据创建 intent,然后将 intent 发送到“通讯录”应用。
使用通讯录应用插入联系人时,系统会将一个新的原始联系人插入到联系人提供程序的 ContactsContract.RawContacts 表中。如有必要,通讯录应用会提示用户输入创建原始联系人时要使用的账号类型和账号。“通讯录”应用还会通知用户原始联系人是否已存在。然后,用户可以选择取消插入,在这种情况下,系统不会创建联系人。如需详细了解原始联系人,请参阅联系人提供程序 API 指南。
创建意图
首先,使用操作 Intents.Insert.ACTION 创建一个新的 Intent 对象。
将 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);
此调用会在“通讯录”应用中打开一个屏幕,以便用户输入新的联系人。联系人的账号类型和账号名称会列在屏幕顶部。用户输入数据并点击完成后,系统会显示通讯录应用的联系人列表。用户通过点击返回返回您的应用。
使用 intent 修改已有联系人
如果用户已选择感兴趣的联系人,则使用 Intent 修改现有联系人会很有用。例如,如果某个应用可查找具有邮政地址但缺少邮政编码的联系人,则该应用可为用户提供查找邮政编码并将其添加到联系人的选项。
如需使用 intent 修改现有联系人,请使用与插入联系人类似的过程。按照使用 intent 插入新联系人部分中的说明创建 intent,但要向 intent 添加联系人的 Contacts.CONTENT_LOOKUP_URI 和 MIME 类型 Contacts.CONTENT_ITEM_TYPE。如果您想使用已有的详细信息修改联系人,可以将这些信息放入 intent 的扩展数据中。请注意,某些名称列无法使用 intent 进行修改;这些列在 ContactsContract.Contacts 类的 API 参考文档的摘要部分中列出,标题为“更新”。
最后,发送该 intent。作为响应,“通讯录”应用会显示修改屏幕。当用户完成修改并保存修改内容后,“通讯录”应用会显示联系人列表。当用户点击返回时,系统会显示您的应用。
创建 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 以处理内部操作,该 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,并且用户修改并保存联系人后,用户点击返回时会看到联系人列表界面。如需返回到您的应用,用户必须点击最近,然后选择您的应用。
如需在 Android 4.0.3(API 版本 15)及更高版本中解决此问题,请向 intent 添加值为 true 的扩展数据键 finishActivityOnSaveCompleted。
Android 4.0 之前的 Android 版本接受此键,但没有任何作用。如需设置扩展数据,请执行以下操作:
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 插入或修改
您可以通过发送具有 ACTION_INSERT_OR_EDIT 操作的 Intent,允许用户选择是插入联系人还是修改现有联系人。例如,电子邮件客户端应用可以允许用户将传入的电子邮件地址添加到新联系人,或将其添加为现有联系人的其他地址。将此 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);