Skip to content

Instantly share code, notes, and snippets.

@reuniware
Created October 15, 2019 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reuniware/53cbee352341a098c3f875d60cd79c55 to your computer and use it in GitHub Desktop.
Save reuniware/53cbee352341a098c3f875d60cd79c55 to your computer and use it in GitHub Desktop.
Android : Get website address from phone contact
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