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) {
have permission to get contact actions
}
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 Conta Ct.setname (cursor.getstring (0)); 6 Contact.setphone (cursor.getstring (1)), 7 lists.add (Contact), 8 } 9 cursor.close ();
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); 8 URI = uri.parse ("content://com.android.contacts/contacts/" + contactId + "/data"); 9 Cursor Cursor1 = Resolver.query (URI, new string[]{"MimeType", "data1"}, NULL, NULL, NULL); 10 if (Cursor1! = null && cursor1.getcount () > 0) {One while (Cursor1.movetonext ()) {12 String mimeType = cursor1.getstring (0), String data1 = cursor1.getstring (1) if ("Vnd.android.cursor.item/name". Equals (MimeType)) {//is the name 15 Contact.setname (data1); + Else if ("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) {//Phone contact.setphone (data1); 18}19}20 Cursor1.close (); Lists.add (contact); 22}23}24 Cursor.close (); 25}
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 Contact contact = new Contact (); 7 String id = cursor1.get String (0); 8 Cursor Cursor2 = Context.getcontentresolver (). Query (Uri4data, new string[]{"Data1", "MimeType"}, "Row_contac t_id =? ", new String[]{id}, NULL); 9 if (Cursor2.movetonext ()) {Ten String data = cursor2.getstring (0); one string m Imetype = cursor2.getstring (1), if (Mimetype.equals ("Vnd.android.cursor.item/name")) {13 Contact.setname (data), or else if (mimetype.equals ("Vnd.android.cursor.item/phone_v2")) {15 Contact.setphone (data); 16}17}18}
Get Contacts for Android