Read phone contacts in Android

Source: Internet
Author: User

The main thing is to use content parser to deal with the problem:

First, you should understand the Android phone contact database: Read the main use of 3 tables:

Table Name Role
Contacts Record Contact ID, (there are a few columns that indicate several contacts).
Mimetypes Storage type alignment (in order to save the database space)
Data The contact information is saved in this table

But in the process of checking, the first time we were looking at the table contacts, the second time we looked at View_data this view

The first loop has several contacts executed several times during the query, but the second cycle is performed up to 11 times per contact, depending on your contact store information

A piece of code drifts through:

Contentresolver Contentresolver =Getcontentresolver (); //querying with the cursor objectcursor cursor =contentresolver.query (Uri. Parse ("Content://com.android.contacts/raw_contacts"),                         NewString[] {"contact_id"},NULL,//query Condition "a=?"                        NULL,//provide a value for the question mark                        NULL); //Circular Cursors                 while(Cursor.movetonext ()) {//The cursor's index value starts at 0.String id = cursor.getstring (0); //query data tables and mimetype generated views based on uniqueness ID values, get data and MimeType fieldsCursor Indexcursor =contentresolver.query (Uri.parse ("Content://com.android.contacts/data"),                            NewString[] {"Data1", "MimeType" },                            "Raw_contact_id=?",NewString[] {ID},NULL); //5 looping through cursor values the query process is still unclear.Hashmap<string, string> HM =NewHashmap<string, string>();  while(Indexcursor.movetonext ()) {//System.out.println (indexcursor.getstring (0) + "" + indexcursor.getstring (1));Hm.put (indexcursor.getstring (1), indexcursor.getstring (0));                    } contactlist.add (HM);                Indexcursor.close (); }                //Close CursorsCursor.close (); //sending updated UI data in a child threadMessage msg =NewMessage (); Msg.what= 1; Mhandler.sendmessage (msg);

Read phone contacts in Android

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.