今天讲通讯录的增改查(没有删)的几个Action,分别是ACTION_INSERT、ACTION_EDIT、ACTION_VIEW。另外,还会讲下他们和前面讲的ACTION_PICK的区别,后者不需要在AndroidManifest.xml中声明访问通讯录的权限,如READ_CONTACTS。
View a contact
To display the details for a known contact, use the ACTION_VIEW action and specify the contact with a content: URI as the intent data.
There are primarily two ways to initially retrieve the contact's URI:
Use the contact URI returned by the ACTION_PICK, shown in the previous section (this approach does not require any app permissions).
Access the list of all contacts directly, as described in Retrieving a List of Contacts (this approach requires the READ_CONTACTS permission).
Action
ACTION_VIEW
Data URI Scheme
content:<URI>
MIME Type
None. The type is inferred from contact URI.
Example intent:
public void viewContact(Uri contactUri) {
Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
To edit a known contact, use the ACTION_EDIT action, specify the contact with a content: URI as the intent data, and include any known contact information in extras specified by constants in ContactsContract.Intents.Insert.
There are primarily two ways to initially retrieve the contact URI:
Use the contact URI returned by the ACTION_PICK, shown in the previous section (this approach does not require any app permissions).
Access the list of all contacts directly, as described in Retrieving a List of Contacts (this approach requires the READ_CONTACTS permission).
Action
ACTION_EDIT
Data URI Scheme
content:<URI>
MIME Type
The type is inferred from contact URI.
Extras
One or more of the extras defined in ContactsContract.Intents.Insert so you can populate fields of the contact details.
Example intent:
public void editContact(Uri contactUri, String email) {
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(contactUri);
intent.putExtra(Intents.Insert.EMAIL, email);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
For more information about how to edit a contact, read Modifying Contacts Using Intents.
Insert a contact
To insert a new contact, use the ACTION_INSERT action, specify Contacts.CONTENT_TYPE as the MIME type, and include any known contact information in extras specified by constants in ContactsContract.Intents.Insert.
Action
ACTION_INSERT
Data URI Scheme
None
MIME Type
Contacts.CONTENT_TYPE
Extras
One or more of the extras defined in ContactsContract.Intents.Insert.
Example intent:
public void insertContact(String name, String email) {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(Contacts.CONTENT_TYPE);
intent.putExtra(Intents.Insert.NAME, name);
intent.putExtra(Intents.Insert.EMAIL, email);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
For more information about how to insert a contact, read Modifying Contacts Using Intents.