Home > Blockchain >  How to get the only phone number from the device contact, not whats app and duo number, etc
How to get the only phone number from the device contact, not whats app and duo number, etc

Time:10-19

How to get the only phone number from the device contact, not whats app and duo number, etc...

Example: I got the device contact number, but that return

  1. Phone Number
  2. What's App Number
  3. Google Duo Number etc...

Note: I need only a phone number from the local device contact.

val phones: Cursor? = contentResolver.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID   " = ?",
        arrayOf(contactId),
        null
    )

// This will return list of contact number
val phoneNumbers: Int = it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)

CodePudding user response:

Reference:- https://developer.android.com/training/contacts-provider/retrieve-details

The above Link has - ContactsContract.Data.DATA1 that only retrieves the phone number checkout above link for more detail

CodePudding user response:

try this one i hope this will help you.

//ContactsContract defines an extensible database of contact-related information
    var uri = ContactsContract.Data.CONTENT_URI

    val PROJECTION = arrayOf(
        ContactsContract.Data.CONTACT_ID,
        ContactsContract.Data.DISPLAY_NAME,
        ContactsContract.Data.PHOTO_URI,
        ContactsContract.Data.DATA1
    )


    var cursor =
        contentResolver.query(uri, PROJECTION, ContactsContract.Data.MIMETYPE   " = ?", arrayOf(
            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
        ), ContactsContract.Contacts.DISPLAY_NAME   " ASC ", null)

    while (cursor.moveToNext())
    {
        val contactId =
            cursor!!.getString(cursor!!.getColumnIndex(ContactsContract.Data.CONTACT_ID))
        val photo = cursor!!.getString(cursor!!.getColumnIndex(ContactsContract.Data.PHOTO_URI))
        val name = cursor!!.getString(cursor!!.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
        val phone = cursor!!.getString(cursor!!.getColumnIndex(ContactsContract.Data.DATA1))
    }
  • Related