Android 2.0 reads the name and phone number of all contacts

Source: Internet
Author: User

 

The code for reading the names and phone numbers of all contacts on the internet is as follows:

ContentResolver cr = getContentResolver ();

Cursor cursor = cr. query (ContactsContract. Contacts. CONTENT_URI, null );

While (cursor. moveToNext ())

{

// Obtain the contact name

Int nameFieldColumnIndex = cursor. getColumnIndex (PhoneLookup. DISPLAY_NAME );

String name = cursor. getString (nameFieldColumnIndex );

String + = (name );

 

// Obtain the contact ID

String contactId = cursor. getString (cursor. getColumnIndex (ContactsContract. Contacts. _ ID ));

Cursor phone = cr. query (ContactsContract. CommonDataKinds. Phone. CONTENT_URI, null, ContactsContract. CommonDataKinds. Phone. CONTACT_ID + "="

+ ContactId, null, null );

 

// Obtain the phone number (multiple numbers may exist)

While (phone. moveToNext ())

{

String strPhoneNumber = phone. getString (phone. getColumnIndex (ContactsContract. CommonDataKinds. Phone. NUMBER ));

String + = (":" + strPhoneNumber );

}

String + = "\ n ";

Phone. close ();

}

Cursor. close ();

If there are n contacts and each contact has a phone number, you have to query n + 1 times.

I saw a post in the garden saying that I could use

Uri. withAppendedPath (PhoneLookup. CONTENT_FILTER_URI ,"*")

After obtaining the information of all contacts, I did not test successfully on the Android 4.0 simulator and the Android simulator.

Various types of contact information are stored in the Data table. Therefore, you can query the Data table and limit its MIMETYPE to Phone. CONTENT_ITEM_TYPE to find all names and Phone numbers.

Cursor phone = cr. query (ContactsContract. CommonDataKinds. Phone. CONTENT_URI, new String [] {

CommonDataKinds. Phone. NUMBER, CommonDataKinds. Phone. DISPLAY_NAME}, null, null );

The above code can be used to check the names and phone numbers of all contacts. However, if two phone numbers are stored by one user, there will be two records in the Data table, for example, a person named Michael stored two phone numbers: 11111,22222. There will be two records about Michael Jacob in the output result, and they will not be merged together. So I want to store all the data queried by cursor in Map and use DISPLAY_NAME as the key, A List composed of numbers is the value, that is

HashMap <String, ArrayList <String>

The following code is available:

ContentResolver cr = getContentResolver ();

HashMap <String, ArrayList <String> hs = new HashMap <String, ArrayList <String> ();

Cursor phone = cr. query (ContactsContract. CommonDataKinds. Phone. CONTENT_URI, new String [] {

CommonDataKinds. Phone. NUMBER, CommonDataKinds. Phone. DISPLAY_NAME}, null, null );

While (phone. moveToNext ()){

String strPhoneNumber = phone. getString (phone. getColumnIndex (ContactsContract. CommonDataKinds. Phone. NUMBER ));

String name = phone. getString (phone. getColumnIndex (CommonDataKinds. Phone. DISPLAY_NAME ));

ArrayList <String> ad = hs. get (name );

If (ad = null ){

Ad = new ArrayList <String> ();

Ad. add (strPhoneNumber );

Hs. put (dis, ad );

}

Else

Ad. add (strPhoneNumber );

}

Phone. close ();

 

In this way, the problem that a name corresponds to multiple numbers can be solved, but there is still a problem that two contacts may have the same name, but they belong to different contacts, and the database shows that they have different contact_ids, you can modify the above Code and add ContactsContract to the projection parameter. commonDataKinds. phone. CONTACT_ID. Then, change Map to contact_id, and use the LIST consisting of DISPLAY_NAME and NUMBER as values. The DISPLAY_NAME is stored as the first item of LIST. Of course, you can also define a class that contains a LIST field consisting of the Name field and phone number. The elements in the LIST of phone numbers can also be Map, with the TYPE of the number as the key.

 

Author: angeldedevil

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.