Android reads mobile phone address book

Source: Internet
Author: User

Android reads mobile phone address book

Chen yuefeng, Java Studio

From: http://blog.csdn.net/mailbomb

Note: Please indicate the source for reprinting.

 

In Android development, reading the phone number in the mobile phone address book is a basic operation. However, due to the large number of Android versions, the operation code in the mobile phone address book is complicated and summarized in this article.

Android1.5 is the lowest version in the current Android system. First, let's talk about the code implementation for android1.5 and later versions (including 2.x, 3.x:

// Obtain all contacts

Cursor cur = context. getcontentresolver (). Query (

Contacts. People. content_uri,

Null,

Null,

Null,

Contacts. People. display_name + "collate localized ASC ");

// Loop Traversal

If (cur. movetofirst ()){

Int idcolumn = cur. getcolumnindex (contacts. People. _ id );

Int displaynamecolumn = cur. getcolumnindex (contacts. People. display_name );

Do {

// Obtain the contact ID

String contactid = cur. getstring (idcolumn );

// Obtain the contact name

String displayname = cur. getstring (displaynamecolumn );

// Obtain the contact's phone number

Cursorphonescur = context. getcontentresolver (). Query (

Contacts. Phones. content_uri, null,

Contacts. Phones. person_id + "=" + contactid, null, null );

If (phonescur. movetofirst ()){

Do {

// Traverse all phone numbers

Stringphonetype = phonescur. getstring (phonescur

. Getcolumnindex (contacts. phonescolumns. Type ));

String phonenumber = phonescur. getstring (phonescur

. Getcolumnindex (contacts. phonescolumns. Number ));

// Your own logic processing code

} While (phonescur. movetonext ());

}

} While (cur. movetonext ());

}

Cur. Close ();

This code can be used to read the phone numbers in the address book of the mobile phone in various versions of Android phones and read multiple numbers under the same name. the efficiency in Version X is not high, and the reading time is longer, and 2. X is currently the mainstream of Android systems, with at least 80% of Android mobile phone shares. Therefore, you can use APIs of higher versions for efficient reading.

The code for reading Address Book for android2.0 and later versions is as follows:

// Read the local phone number of the mobile phone

Contentresolver Cr = context. getcontentresolver ();

// Obtain the cursor of the Start item in the phone book. You must first movetonext ()

Cursor cursor = Cr. Query (contactscontract. Contacts. content_uri, null );

While (cursor. movetonext ()){

// Retrieve the contact name index

Int nameindex = cursor. getcolumnindex (phonelookup. display_name );

String name = cursor. getstring (nameindex );

// Obtain the contact's ID index value

String contactid = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. _ id ));

// Query the phone number of the contact. For example, you can query email and photo.

Cursor phone = Cr. Query (contactscontract. commondatakinds. Phone. content_uri, null,

Contactscontract. commondatakinds. Phone. contact_id + "="

+ Contactid, null, null); // The first parameter is the phone number to be queried, and the third parameter is the filtering value of a specific person.

// A person may have several numbers

While (phone. movetonext ()){

String phonenumber = phone. getstring (phone. getcolumnindex (contactscontract. commondatakinds. Phone. Number ));

Listname. Add (name );

Listphone. Add (phonenumber );

}

Phone. Close ();

}

Cursor. Close ();

To read the address book in the SIM card, you can use "content: // ICC/ADN" to read the address book. The Code is as follows:

Try {

Intent intent = new intent ();

Intent. setdata (URI. parse ("content: // ICC/ADN "));

Uri uri = intent. getdata ();

Contentresolvercr = context. getcontentresolver ();

Cursor cursor = context. getcontentresolver (). Query (Uri, null );

If (cursor! = NULL ){

While (cursor. movetonext ()){

// Retrieve the contact name index

Int nameindex = cursor. getcolumnindex (phonelookup. display_name );

String name = cursor. getstring (nameindex );

// Obtain the contact's ID index value

String contactid = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. _ id ));

// Query the phone number of the contact. For example, you can query email and photo.

Cursor phone = Cr. Query (contactscontract. commondatakinds. Phone. content_uri, null,

Contactscontract. commondatakinds. Phone. contact_id + "="

+ Contactid, null, null); // The first parameter is the phone number to be queried, and the third parameter is the filtering value of a specific person.

// A person may have several numbers

While (phone. movetonext ()){

String phonenumber = phone. getstring (phone. getcolumnindex (contactscontract. commondatakinds. Phone. Number ));

// Your own logic code

}

Phone. Close ();

}

Cursor. Close ();

}

} Catch (exception e ){}

 

Hope to help you!

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.