How to get a contact using contactscontract in Android

Source: Internet
Author: User

Since Android 2.0 SDK, the class related to the contact provider has changed to contactscontract, although the old android. provider. contacts can be used, but the method marked as deprecated In the SDK will be abandoned and not recommended, and Android is added since Android 2.0 and API Level 5. provider. contactscontract to replace the original method.

Contactscontract subclass contactscontract. Contacts is a table that represents the statistics of all contacts. For example, contact ID (-ID), query key (lookup_key), contact name (display_name_primary), Avatar ID (photo_id), and group ID.

You can use the following method to obtain the cursor object of all tables of contacts:

1) contentresolver = getcontentresolver (); // obtain the shared object defined in contentresolver object query;

2) cursor = contentresolver. Query (contactscontract. Contacts. content_uri, null); // query all contacts based on the URI object contactscontract. Contacts. content_uri;

From the cursor object, the key is to obtain the contact's _ id. It then queries the phone number (contactscontract. commondatakinds. commondatakinds. Phone) and email (contactscontract. commondatakinds. Email) of the _ id contact through the Child classes of contactscontract.

Take obtaining all the phone numbers of the contact as an example:

1) int idfieldindex = cursor. getcolumnindex (contactscontract. Contacts. _ id );

Int id = cursor. getint (idfieldindex); // obtain the contact id based on the column name;

2) cursor phonecursor = contentresolver. Query (contactscontract. commondatakinds. Phone. content_uri, null, contactscontract. commondatakinds. Phone. contact_id + "=? ", New string [] {INTEGER. tostring (ID)}, null); // query all the phone numbers of contacts with the corresponding ID in contactscontract. commondatakinds. Phone;

Similarly, different sub-classes of contactscontract. commondatakinds can be used to query different contents. The android document tells us that we recommend using contactscontract. Contacts. lookup_key instead of contactscontract. Contacts. _ id.

Finally, in order to improve user experience, the resources occupied by reading contacts are compared. We recommend that you use the asynctask class.

When reading the address book of the Android system, the contacts are generally read first and then their numbers are read in a nested loop. If the number of contacts in the address book is small, the speed is acceptable, but the number of people in the address book is 1 to 2, I am afraid it will be slow. If the hardware is almost worse, the experience will be worse. Available

Contactscontract. commondatakinds. Phone. content_uri (corresponding to the contacts2.db data view view_data_restricted) view to avoid nested reading, but phonelookup. content_filter_uri cannot be directly used

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.