Android Question help selected contact

roy89

Member
Hola, intento abrir el selector de contactos en mi teléfono y que me devuelva el contacto seleccionado para que se agregue a mi edittext4, pero no pasa nada. Selecciono el contacto y no pasa nada. ¿Pueden ayudarme?:
Sub Button51_Click
rp.CheckAndRequest(rp.PERMISO_LEER_CONTACTOS)
Esperar Activity_PermissionResult (permiso como cadena, resultado como booleano)

Si Resultado Entonces
Dim i como intención
i.Initialize("android.intent.action.PICK", "contenido://com.android.contactos/datos/teléfonos")
Iniciar actividad(i)
Fin si
Fin del subtítulo

SubActividad_Resultado (Código de solicitud como entero, Código de resultado como entero, Datos como intención)
Si ResultCode = -1 y Data.IsInitialized entonces
Intentar
Dim cr como ContentResolver
cr.Inicializar("")

Atenuar uriString como cadena = Datos.ObtenerDatos
Dimensiones Ases
uri.Parse(uriString)

Dim c como cursor = cr.Query(uri, matriz como cadena("data1"), "", Null, "")

Si c.RowCount > 0 Entonces
c.Posición = 0
Dim phoneNumber como cadena = c.GetString2(0)
c.Cerrar

Atenuar cleanNumber como cadena = ""
Para i = 0 A phoneNumber.Length - 1
Dígito atenuado como cadena = phoneNumber.CharAt(i)
Si dígito >= "0" Y dígito <= "9" Entonces cleanNumber = cleanNumber & dígito
Próximo

Si cleanNumber.Length > 8 Entonces
cleanNumber = cleanNumber.SubString(cleanNumber.Length - 8)
Fin si

EditText4.Texto = cleanNumber
ToastMessageShow("Número: " & cleanNumber, Falso)
Demás
c.Cerrar
Fin si

Atrapar
Registro(Última excepción.Mensaje)
Fin del intento
Fin si
Fin Sub
 

zed

Well-Known Member
Licensed User
You are on the English section of the forum; Please post your messages in English.
We cannot understand what you are doing.
 
Upvote 0

roy89

Member
You are on the English section of the forum; Please post your messages in English.
We cannot understand what you are doing.
"Hello, I'm trying to open the contact picker on my phone and have it return the selected contact so it can be added to my EditText4, but nothing happens. I select the contact and nothing happens. Can you help me? Sorry, it's just that I'm using a translator and I got confused with the language — it was supposed to be in English but I ended up writing it in Spanish."**:
Sub Button51_Click
    rp.CheckAndRequest(rp.PERMISSION_READ_CONTACTS)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)

    If Result Then
        Dim i As Intent
        i.Initialize("android.intent.action.PICK", "content://com.android.contacts/data/phones")
        StartActivity(i)
    End If
End Sub

Sub Activity_Result (RequestCode As Int, ResultCode As Int, Data As Intent)
    If ResultCode = -1 And Data.IsInitialized Then
        Try
            Dim cr As ContentResolver
            cr.Initialize("")

            Dim uriString As String = Data.GetData
            Dim uri As Uri
            uri.Parse(uriString)

            Dim c As Cursor = cr.Query(uri, Array As String("data1"), "", Null, "")

            If c.RowCount > 0 Then
                c.Position = 0
                Dim phoneNumber As String = c.GetString2(0)
                c.Close

                Dim cleanNumber As String = ""
                For i = 0 To phoneNumber.Length - 1
                    Dim digit As String = phoneNumber.CharAt(i)
                    If digit >= "0" And digit <= "9" Then cleanNumber = cleanNumber & digit
                Next

                If cleanNumber.Length > 8 Then
                    cleanNumber = cleanNumber.SubString(cleanNumber.Length - 8)
                End If

                EditText4.Text = cleanNumber
                ToastMessageShow("Number: " & cleanNumber, False)
            Else
                c.Close
            End If

        Catch
            Log(LastException.Message)
        End Try
    End If
End Sub
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
A slight change to your code but it works ... (There might be more up to date methods availiable to do this...)

I presume you have the following in your Manifest ...
B4X:
AddPermission("android.permission.READ_CONTACTS")
 

Attachments

  • Test Contact Chooser.zip
    10.4 KB · Views: 24
Upvote 0
Top