The code is simple, no more nonsense.
Copy Code code as follows:
Reading contacts
public static Uri Contactsuri = contactscontract.contacts.content_uri;//Contact
public static void Getcontactsinfo (context context,string tag) {
string[] projections = new String[]{contactscontract.contacts._id,contactscontract.contacts.display_name};
Cursor Cursor = Context.getcontentresolver (). Query (Contactsuri, projections, NULL, NULL, NULL);
int albumindex = Cursor.getcolumnindexorthrow (Projections[0]);
int titleindex = Cursor.getcolumnindexorthrow (projections[1]);
LOG.D (tag, cursor.getcount () + "");
while (Cursor.movetonext ()) {
String album = cursor.getstring (Albumindex);
String title = cursor.getstring (Titleindex);
LOG.D (Tag, album+ ":" +title);
}
Cursor.close ();
}
Search for contact information based on contacts
public static void Searchcontacts (context context,string tag) {
String searchname = "Wang";
Uri uri = Uri.withappendedpath (ContactsContract.Contacts.CONTENT_FILTER_URI, searchname);
Uri uri2 = Uri.withappendedpath (ContactsContract.PhoneLookup.CONTENT_FILTER_URI, PhoneNumber); Find a contact by phone number
string[] projection = new string[]{contactscontract.contacts._id};
Cursor Cursor = Context.getcontentresolver (). Query (URI, projection, NULL, NULL, NULL);
String id = null;
if (Cursor.movetofirst ()) {
id = cursor.getstring (cursor.getcolumnindexorthrow (contactscontract.contacts._id));
}
Cursor.close ();
if (id!=null) {
String where = contactscontract.data._id+ "=" +id;
projection = new String[]{contactscontract.data.display_name,contactscontract.commondatakinds.phone.number};
Cursor searchccursor = Context.getcontentresolver (). Query (ContactsContract.Data.CONTENT_URI, projection, where, null , null);
LOG.D (tag, searchccursor.getcount () + "");
int nameindex = Searchccursor.getcolumnindex (projection[0]);
int numberindex = Searchccursor.getcolumnindex (projection[1]);
while (Searchccursor.movetonext ()) {
String name = searchccursor.getstring (Nameindex);
String number = searchccursor.getstring (Numberindex);
LOG.D (Tag, number+ ":" +name);
}
Searchccursor.close ();
}
}
The above is the article to share the entire content of the code, I hope you can enjoy.