Android realizes code to read and search contacts _android

Source: Internet
Author: User

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.

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.