Created
October 15, 2019 10:20
-
-
Save reuniware/53cbee352341a098c3f875d60cd79c55 to your computer and use it in GitHub Desktop.
Android : Get website address from phone contact
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getPhoneContacts() : List<PhoneContact> { | |
phoneContactsList = ArrayList<PhoneContact>() | |
if (!hasPhoneContactsPermission(Manifest.permission.READ_CONTACTS)) { | |
requestReadContactsPermission(Manifest.permission.READ_CONTACTS) | |
} else { | |
//showOkDialog("permissions contacts", "permissions accordées", this as AppCompatActivity, false) | |
val cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null) | |
if (cursor != null) { | |
if (cursor.count > 0) { | |
while (cursor.moveToNext()) { | |
val phoneContact = PhoneContact() | |
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)) | |
phoneContact.id = id | |
val name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) | |
phoneContact.name = name | |
val phoneNumber = (cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))).toInt() | |
if (phoneNumber > 0) { | |
val cursorPhone = contentResolver.query( | |
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, | |
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", arrayOf(id), null) | |
if (cursorPhone != null) { | |
if (cursorPhone.count > 0) { | |
while (cursorPhone.moveToNext()) { | |
val phoneNumValue = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) | |
Timber.i("name = $name ; contact phone = $phoneNumValue") | |
phoneContact.phoneNumbers.add(phoneNumValue) | |
} | |
} | |
cursorPhone.close() | |
} | |
} | |
val emails = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + id, null, null) | |
while (emails!!.moveToNext()) { | |
val emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)) | |
Timber.i("name = $name ; email address = $emailAddress") | |
phoneContact.emailAddresses.add(emailAddress) | |
} | |
emails.close() | |
val postalAddresses = contentResolver.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + id, null, null) | |
while (postalAddresses!!.moveToNext()) { | |
val postalAddress = postalAddresses.getString(postalAddresses.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA)) | |
Timber.i("name = $name ; postal address = $postalAddress") | |
phoneContact.addresses.add(postalAddress) | |
} | |
val projection: Array<out String> = arrayOf(ContactsContract.CommonDataKinds.Website.URL, ContactsContract.CommonDataKinds.Website.TYPE) | |
val selection = ContactsContract.Data.CONTACT_ID + " = " + id + " AND " + ContactsContract.Contacts.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'" | |
val contactData = contentResolver.query(ContactsContract.Data.CONTENT_URI, projection, selection, null, null) | |
while (contactData!!.moveToNext()) { | |
Timber.i("website URL = ${contactData.getString(0)}") | |
} | |
phoneContact.addObserver(MyObserver(recyclerViewPhoneContacts)) | |
phoneContactsList.add(phoneContact) | |
} | |
} | |
cursor.close() | |
} else { | |
showOkDialog("importation contacts", "Auncun contact à importer", this as AppCompatActivity, false) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment