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