[Learn Android development every day] use the new contacts API (contactscontract) to read contact information

Source: Internet
Author: User

Today's learning task: Read the main information (name and phone number) of all contacts on the mobile phone, and display it in listview

Main Content involved: 1) structure and usage of the contact API 2) role of the contentprovider component, Uri concept and usage 3) create a listview and bind data

 

1. Structure and usage of the contact API

Since Android 2.0 (API Level 5), the Android platform has adopted the improved contacts API-contactscontract to manage and integrate contact information from multiple accounts and multiple data sources.

In the new contacts API, contact data is arranged in three main tables: contacts, raw contacts and data. The structure is shown in:

(Image Source: pai.android.com)

1) a row of records in the contact table indicates the overall information of a contact.

2) a row of records in the rawcontact table is used to associate a contact with a specific contact information source. It is possible that the mobile phone contact information comes from Gmail, Facebook, and other places. rawcontact is introduced for Mutual Difference and convenience of synchronization.

3) data table: stores all specific information, such as phone number, email address, and profile picture. Each record in the table corresponds to a specific rawcontact information.

In general, a contact record is associated with one or more rawcontact records. Each rawcontact record is associated with multiple data records (such as email and phone number.

Reference 1: http://developer.android.com/resources/articles/contacts.html

Reference 2: http://www.haoni.org/2011/04/15/androidcontactscontractyanjiu/

2. Role of the contentprovider component, Uri concept and usage

In AndroidProgramThey are independent of each other and run in their own processes. What if applications want to share data with each other? For example, when we send a text message, we may use the Contact application to select the person who wants to receive the text message. In this case, Android provides a unified interface for applications to access each other. These interfaces are defined in contentprovider, including adding, deleting, querying, and modifying operations.

In contentprovider, we implement our actual data operations. However, we will use another interface: contentresolver. Contentresolver provides methods corresponding to contentprovider. We indirectly use contentresolver to operate contentprovider. Contentresolver can be obtained through the getcontentresolver () method.

Uri Description: You can refer to the relevant part of the http://www.cnblogs.com/thomas-lee/archive/2011/04/16/Android3.html and the relevant part of the official documentation, such:

Http://developer.android.com/guide/topics/providers/content-providers.html#creating.

Textutils

 

For details about Android Application Development, Guo Hongzhi and e-Industry Press

3. Create a listview and bind data

Reference: http://developer.android.com/resources/tutorials/views/hello-listview.html

4. FinalCodeAs follows:

Package Com. memo;
Import Java. util. arraylist;
Import Android. App. listactivity;
Import Android. database. cursor;
Import Android.net. Uri;
Import Android. OS. Bundle;
Import Android. provider. contactscontract;
Import Android. widget. arrayadapter;
Import Android. widget. listview;

Public   Class Main Extends Listactivity {

/** Called when the activity is first created. */
@ Override
Public   Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );

Uri contactsuri = Contactscontract. Contacts. content_uri;
String [] proj1 = New String [] {contactscontract. Contacts. display_name,
Contactscontract. Contacts. has_phone_number,
Contactscontract. Contacts. lookup_key };
Cursor curcontacts = Getcontentresolver (). Query (contactsuri, proj1, Null , Null , Null );

// Declare a arraylist object to store the data that will present to the user
Arraylist < String > Contactslist = New Arraylist < String > ();
String allphoneno = "" ;
If (Curcontacts. getcount () > 0 ){
While (Curcontacts. movetonext ()){
// Get all the phone numbers if exist
If (Curcontacts. getint ( 1 ) > 0 ){
Allphoneno = Getallphonenumbers (curcontacts. getstring ( 2 ));
}
Contactslist. Add (curcontacts. getstring ( 0 ) + " , " + Allphoneno );
Allphoneno = "" ;
}
}

// Binding the data to listview
Setlistadapter ( New Arrayadapter < String > ( This , Android. R. layout. simple_list_item_1, contactslist ));
Listview LV = Getlistview ();
LV. settextfilterenabled ( True );

}

/**
* Get all the phone numbers of a specific contact person
*
* @ Param Lookup_key lookup key for a specific contact
* @ Return A string containing all the phone numbers
*/
Public String getallphonenumbers (string lookup_key ){
String allphoneno = "" ;

// Phone info are stored in the contactscontract. Data Table
Uri phoneuri = Contactscontract. commondatakinds. Phone. content_uri;
String [] proj2 = {Contactscontract. commondatakinds. Phone. Number };
// Using lookup key to search the phone numbers
String Selection = Contactscontract. Data. lookup_key + " =? " ;
String [] selectionargs = {Lookup_key };
Cursor cur = Getcontentresolver (). Query (phoneuri, proj2, selection, selectionargs, Null );
While (Cur. movetonext ()){
Allphoneno + = Cur. getstring ( 0 ) + "   " ;
}

ReturnAllphoneno;

}

5. Notes:

1) to read the contact information, you must add the related uses-permission in androidmenifest. XML to request a license:

<Uses-Permission Android: Name = "android. Permission. read_contacts"/>

Reference 1: http://developer.android.com/guide/topics/security/security.html

Reference 2: http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

2) how to obtain the phone number of a specific contact? Because the new API is used, the writing method is different from using the old API.

Lookup key is used in queries.

Reference: http://stackoverflow.com/questions/4729551/contactscontract-lookup-phone-number-by-contact-id

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.