Implement a simple dialer program (1)-Get all contacts

Source: Internet
Author: User

In order to be able to display all possible matching contacts in real time when dialing (in fact, no use, if you remember the number is much faster than to find in the Address book), first write a system to obtain the contacts of the ContentProvider method, Here's how I find all my contacts ' names and phone numbers:

1 voidsearch () {2Contentresolver CR =getcontentresolver ();3 4string[] Projection =Newstring[]{5 contactscontract.contacts._id6         };7 8Cursor idcursor = Cr.query (Contactscontract.contacts.content_uri,projection,NULL,NULL,NULL);9 Ten String ID; One          while(Idcursor.movetonext ()) { A             intIdidx =Idcursor.getcolumnindexorthrow (contactscontract.contacts._id); -Id=idcursor.getstring (IDIDX); - printcontacts (ID); the         } -  - idcursor.close (); -     } +  -     voidprintcontacts (String id) { +         if(id!=NULL){ AString where = contactscontract.data.contact_id+ "=" +id + "+" + contactscontract.data.mimetype+ "= '" + contactscontract . Commondatakinds.phone.content_item_type+ "'"; at  -string[] Projection =Newstring[]{ - ContactsContract.Data.DISPLAY_NAME, - ContactsContract.CommonDataKinds.Phone.NUMBER -             }; -  inCursor datacursor = Getcontentresolver (). Query (Contactscontract.data.content_uri,projection,where,NULL,NULL); -  to             intNameidx =Datacursor.getcolumnindexorthrow (ContactsContract.Data.DISPLAY_NAME); +             intPhoneidx =Datacursor.getcolumnindexorthrow (ContactsContract.CommonDataKinds.Phone.NUMBER); -  theString[] result =NewString[datacursor.getcount ()]; *  $              while(Datacursor.movetonext ()) {Panax NotoginsengString name =datacursor.getstring (NAMEIDX); -String number =datacursor.getstring (PHONEIDX); theNumber = Number.replaceall ("+", ""); +Result[datacursor.getposition ()] = name + "{" +number+ "}"; A             } the  + datacursor.close (); -  $              for(String item:result) { $LOG.I ("TAG", item); -             } -         } the}

The above code comes from Android 4 advanced programming.

The overall process is by querying the ID of each contact information and then querying the contact information associated with the program through another query.

You can also get a contact's avatar thumbnail, just add it to the projection:

New string[]{                    ContactsContract.Data.DISPLAY_NAME,                    ContactsContract.CommonDataKinds.Phone.NUMBER,                    ContactsContract.Data.PHOTO_THUMBNAIL_URI            };

The last condition, and then normally get the string type of Imageuri:

intNameidx =Datacursor.getcolumnindexorthrow (ContactsContract.Data.DISPLAY_NAME); intPhoneidx =Datacursor.getcolumnindexorthrow (ContactsContract.CommonDataKinds.Phone.NUMBER); intthumbnail =Datacursor.getcolumnindexorthrow (ContactsContract.Data.PHOTO_THUMBNAIL_URI); String[] Result=NewString[datacursor.getcount ()];  while(Datacursor.movetonext ()) {String name=datacursor.getstring (NAMEIDX); String Number=datacursor.getstring (PHONEIDX); String Thumb=datacursor.getstring (thumbnail); number= Number.replaceall ("+", "" "); Result[datacursor.getposition ()]= name + "{" + number + "}," +thumb; }

The ImageView Setimageuri () method allows you to use the local image URI directly, such as:

Imageview.setimageuri (Uri.parse (thumb));

Privacy-related, the test results on my phone are no longer displayed here.

The next plan is to detach the results of the query and display it in a ListView in the dial-up interface.

Implement a simple dialer program (1)-Get all contacts

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.