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