android developer tiny share-20160819

本文介绍了如何使用Android系统提供的ACTION_VIEW、ACTION_EDIT和ACTION_INSERT来查看、编辑和插入联系人。这些操作可以通过Intent传递特定的content URI来指定目标联系人,并且在编辑或插入联系人时还可以通过ContactsContract.Intents.Insert定义的extra参数来填充联系人信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天讲通讯录的增改查(没有删)的几个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);
    }
}


Edit an existing contact
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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值