Get Contacts for Android

Source: Internet
Author: User

Get started with Android, record the problems encountered in learning and some personal summaries.

Contact database path is:/data/data/com.android.providers.contacts/database/contacts2.db

Permissions issues: Android 6.0 or more just write permissions in XML is not enough, the code also needs to write.
 if  (Build.VERSION.SDK_INT >= build.version_codes.   M && checkselfpermission (Manifest.permission.READ_CONTACTS)! = packagemanager.permission_granted) {
Requestpermissions (new string[]{manifest.permission.read_contacts}, 10);
}

public void Onrequestpermissionsresult (int Requestcode, string[] permissions, int [] grantresults) {
if (Requestcode = ten) {
if (grantresults[0] = = packagemanager.permission_granted) {
//have permission to get contact action
} else {
Toast.maketext (this , "You blocked permissions", Toast.length_short). Show ();
}
}


The first method, the code is also the most concise, sort_key_primary can also be sorted by Chinese name Pinyin, personal recommendation:
1 cursor cursor = context.getcontentresolver (). Query (Phone.content_uri, new String[]{phone.display_name, Phone.number }, NULL, null, phone.sort_key_primary);  2         if (cursor! = null) {             3 while (Cursor.movetonext ()) {4                 Contactbean contact = new Contactbean (); 5                 contact.setname (cursor.getstring (0)), 6                 Contact.setphone (cursor.getstring (1 cursor.close (); 10 }

Second method: You can get relevant information according to the contact ID.

1 uri uri = uri.parse ("Content://com.android.contacts/contacts"); 2 Contentresolver resolver =Context.getcontentresolver (); 3 cursor cursor = Resolver.query (URI, new string[]{"_id"}, NULL, NULL, NULL); 4 if (cursor! = NULL && cursor.getcount () > 0) {5 while (Cursor.movetonext ()) {6 Contact contact = new  contact (); 7 int contactId = Cursor.getint (0< span>); 8 URI = uri.parse ("content://com.android.contacts/contacts/" + contactId + "/data" ); 9 Cursor Cursor1 = Resolver.que Ry (URI, new string[]{"MimeType", "data1"}, NULL, NULL, NULL ); if (Cursor1! = null && cursor1.getcount () &G T 0 ) {One while  (Cursor1.movetonext ()) {MimeType = cursor1.getstring (0 ), string data1 = Cursor1 . getString (1 ); if ("Vnd.android.cursor.item/name". Equals (MimeType)) {//is the name of the  contact.setname (data1); The Else if ("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) {//Phone   contact.setphone (data1);}19 }20  cursor1.close ();  lists.add (contact); }23 }24  cursor.close (); /span>                

Third method: Compared with the second method, only the second URI is different, (PS: This method has a bug, get to some people's data is null, but the total number of contacts is the same.) It is said that because previously deleted contacts, my phone is not root, can not read the database table data, I hope someone can give an explanation)

1 Uri uri4contacts = uri.parse (Contactscontract.authority_uri + "/contacts"); 2 Uri uri4data = uri.parse (Contactscontract.authority_uri + "/data"); 3 list<contact> lists = new arraylist<>(); 4 Cursor Cursor1 = Context.getcontentresolver () . Query (uri4contacts, new string[]{"_id"}, NULL, NULL, NULL); 5 if (Cursor1.movetonext ()) {6 C ontact contact = new Contact (); 7 String id = cursor1.getstring (0); 8 Cursor Cursor2 = Context.getcontentresolver ( ). Query (Uri4data, new string[]{"Data1", "MimeType"}, "row_contact_id =?", new String[]{id}, null); 9 If (Cursor2. MoveToNext ()) {Ten String data = cursor2.getstring (0), one string mimetype = cursor2.getstring (1), and if (Mimetype.eq Uals ("Vnd.android.cursor.item/name")) { contact.setname (data), + else if (mimetype.equals (" Vnd.android.cursor.item/phone_v2 ")) { contact.setphone (data), }17 }18}
                  

Get Contacts for Android

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.