android developer tiny share-20160818

来源:互联网 发布:sql server2008安装包 编辑:程序博客网 时间:2024/05/16 08:40

今天分享如何通过intent访问通讯录,以及如何访问通讯录的某部分数据,如只访问电话号码、只访问邮箱、只访问邮政编码。

Contacts/People App
Select a contact

To have the user select a contact and provide your app access to all the contact information, use the ACTION_PICK action and specify the MIME type to Contacts.CONTENT_TYPE.

The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact. The response grants your app temporary permissions to read that contact using the Contacts Provider API even if your app does not include the READ_CONTACTS permission.

Tip: If you need access to only a specific piece of contact information, such as a phone number or email address, instead see the next section about how to select specific contact data.

Action
    ACTION_PICK
Data URI Scheme
    None
MIME Type
    Contacts.CONTENT_TYPE
Example intent:

static final int REQUEST_SELECT_CONTACT = 1;public void selectContact() {    Intent intent = new Intent(Intent.ACTION_PICK);    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);    if (intent.resolveActivity(getPackageManager()) != null) {        startActivityForResult(intent, REQUEST_SELECT_CONTACT);    }}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) {        Uri contactUri = data.getData();        // Do something with the selected contact at contactUri        ...    }}

For information about how to retrieve contact details once you have the contact URI, read Retrieving Details for a Contact. Remember, when you retrieve the contact URI with the above intent, you do not need the READ_CONTACTS permission to read details for that contact.


Select specific contact data
To have the user select a specific piece of information from a contact, such as a phone number, email address, or other data type, use the ACTION_PICK action and specify the MIME type to one of the content types listed below, such as CommonDataKinds.Phone.CONTENT_TYPE to get the contact's phone number.

If you need to retrieve only one type of data from a contact, this technique with a CONTENT_TYPE from the ContactsContract.CommonDataKinds classes is more efficient than using the Contacts.CONTENT_TYPE (as shown in the previous section) because the result provides you direct access to the desired data without requiring you to perform a more complex query to Contacts Provider.

The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact data. The response grants your app temporary permissions to read that contact data even if your app does not include the READ_CONTACTS permission.

Action
    ACTION_PICK
Data URI Scheme
    None
MIME Type
    CommonDataKinds.Phone.CONTENT_TYPE
        Pick from contacts with a phone number.
    CommonDataKinds.Email.CONTENT_TYPE
        Pick from contacts with an email address.
    CommonDataKinds.StructuredPostal.CONTENT_TYPE
        Pick from contacts with a postal address.
Or one of many other CONTENT_TYPE values under ContactsContract.

Example intent:

static final int REQUEST_SELECT_PHONE_NUMBER = 1;public void selectContact() {    // Start an activity for the user to pick a phone number from contacts    Intent intent = new Intent(Intent.ACTION_PICK);    intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);    if (intent.resolveActivity(getPackageManager()) != null) {        startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);    }}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {        // Get the URI and query the content provider for the phone number        Uri contactUri = data.getData();        String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};        Cursor cursor = getContentResolver().query(contactUri, projection,                null, null, null);        // If the cursor returned is valid, get the phone number        if (cursor != null && cursor.moveToFirst()) {            int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);            String number = cursor.getString(numberIndex);            // Do something with the phone number            ...        }    }}


0 0