Find contacts in your phone

Source: Internet
Author: User

Create query for contacts

 Public classQuery {//ways to query contacts         Public StaticList<contact>Querycontact (Context context) {//Create a Collection objectList<contact> contacts=NewArraylist<contact>(); //Query the Raw_contact table first the table has several contact data//because contact data is exposed through the content provider, we can manipulate the database through content resolutionUri uri=uri.parse ("Content://com.android.contacts/raw_contacts"); Uri Datauri=uri.parse ("Content://com.android.contacts/data"); Cursor Cursor=context.getcontentresolver (). Query (URI,Newstring[]{"contact_id"},NULL,NULL,NULL);  while(Cursor.movetonext ()) {String contact_id=cursor.getstring (0); if(contact_id!=NULL) { Contact Contact=NewContact ();                    Contact.setid (contact_id); Cursor Cursor1=context.getcontentresolver (). Query (Datauri,Newstring[]{"Data1", "MimeType"}, "Raw_contact_id=?",NewSTRING[]{CONTACT_ID},NULL);  while(Cursor1.movetonext ()) {String data1=cursor1.getstring (0); String mimetype=cursor1.getstring (1); //Distinguishing Types                        if("Vnd.android.cursor.item/name". Equals (MimeType))                        {contact.setname (data1); }                        Else if("Vnd.android.cursor.item/phone_v2". Equals (MimeType))                        {Contact.setphone (data1); }                    }                    //put the contact into the collectionContacts.add (Contact); }            }            returncontacts; }}

Create a Contact wrapper class

 Public classContact {PrivateString ID; PrivateString name; PrivateString Phone;  PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getphone () {returnphone; }     Public voidSetphone (String phone) { This. Phone =phone; }}

Find contacts in your phone

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.