Android get contacts and android contacts

Source: Internet
Author: User

Android get contacts and android contacts

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

The contact database path is/data/com. android. providers. contacts/database/contacts2.db.

Permission issues: Android 6.0 and above only have insufficient write permissions in XML and need to be written in the code.
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 = 10 ){
If (grantResults [0] = PackageManager. PERMISSION_GRANTED ){
// You have the permission to obtain contacts.
} Else {
Toast. makeText (this, "you have blocked the permission", Toast. LENGTH_SHORT). show ();
}
}


In the first method, the Code is also the most concise. SORT_KEY_PRIMARY can also be sorted by Chinese name in pinyin. For 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)); 7                 lists.add(contact); 8             } 9             cursor.close();10         }

Method 2: obtain related information based on 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); 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/"+ contactId +"/data "); 9 Cursor cursor1 = resolver. query (uri, new String [] {"mimetype", "data1"}, null); 10 if (cursor1! = Null & cursor1.getCount ()> 0) {11 while (cursor1.moveToNext () {12 String mimeType = cursor1.getString (0); 13 String data1 = cursor1.getString (1 ); 14 if ("vnd. android. cursor. item/name ". equals (mimeType) {// name 15 contact. setName (data1); 16} else if ("vnd. android. cursor. item/phone_v2 ". equals (mimeType) {// mobile phone 17 contact. setPhone (data1); 18} 19} 20 cursor1.close (); 21 lists. add (contact); 22} 23} 24 cursor. close (); 25}

 

Method 3: Compared with method 2, only the second uri is different. (PS: This method has a BUG. Some people's data obtained is NULL, but the total number of contacts is the same. It is said that the contact has been deleted before, And my mobile phone is not ROOT, and the database table data cannot be viewed. 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.getString(0); 8             Cursor cursor2 = context.getContentResolver().query(uri4Data, new String[]{"data1", "mimetype"}, "row_contact_id = ?", new String[]{id}, null); 9             if (cursor2.moveToNext()) {10                 String data = cursor2.getString(0);11                 String mimetype = cursor2.getString(1);12                 if (mimetype.equals("vnd.android.cursor.item/name")) {13                     contact.setName(data);14                 } else if (mimetype.equals("vnd.android.cursor.item/phone_v2")) {15                     contact.setPhone(data);16                 }17             }18         }

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.