Italian Probema con ContactUtils

Lello1964

Well-Known Member
Licensed User
Longtime User
Devo recuperare dalla lista dei contatti del telefono un numero cercando per nome.
Mi basta il primo che corrisponde.

Ho usato la libreria ConatctsUtils di @Erel.

Ho un comportamento anomalo del programma:

In debug funziona dopo il secondo avvio, in release mi da sempre errore :

Errore:
main_trovanumero (java line: 624)
java.lang.ClassCastException: b4a.example.contactsutils$_cucontact cannot be cast to b4a.example.main$_tcucontact
    at b4a.example.main._trovanumero(main.java:624)
    at b4a.example.main._button1_click(main.java:464)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7185)
    at android.view.View.performClickInternal(View.java:7162)
    at android.view.View.access$3500(View.java:819)
    at android.view.View$PerformClick.run(View.java:27678)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7590)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Non capisco il problema.

Main:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Dim cu As ContactsUtils
    Dim rp As RuntimePermissions
 
    Type TcuContact (Id As Long, DisplayName As String)
    Type TcuPhone (Number As String, PhoneType As String)
End Sub

Sub Globals
    Dim NomeDaTrovare As String
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
 
    Wait For (CheckPermissions) Complete (Success As Boolean)
    If Success Then
        cu.Initialize
    Else
        ToastMessageShow("No permission", True)
    End If
 
    ' Inserisci il numero che vuoi cercare nella tua rubrica.
 
    NomeDaTrovare = "Raffaele"
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    MsgboxAsync("Numero di Telefono : " & TrovaNumero(NomeDaTrovare),"Trovato")
End Sub

Sub CheckPermissions As ResumableSub
    For Each per In Array(rp.PERMISSION_READ_CONTACTS, rp.PERMISSION_WRITE_CONTACTS)
        rp.CheckAndRequest(per)
        Wait For Activity_PermissionResult (permission As String, Result As Boolean)
        If Result = False Then Return False
    Next
    Return True
End Sub

Sub TrovaNumero(nome As String) As String
    For Each c As TcuContact In cu.FindContactsByName(nome.Trim, True, False)
        Log (c.DisplayName & ".")
        For Each phone As TcuPhone In cu.GetPhones(c.Id)
                Return phone.Number
        Next
    Next
    Return "NON TROVATO !"
End Sub


Allego il progetto.
 

Attachments

  • CONATTI.zip
    12.9 KB · Views: 106
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
L'esempio funziona fino a SDK 28 (ho provato il tuo esempio in release su un dispositivo con Android 7). Poi non può funzionare, non è più permesso accedere a contatti.

Hai scaricato l'ultima versione di Contacts? Con la versione 1.20 dovrebbe funzionare anche con SDK 29
 

Xfood

Expert
Licensed User
Che palle google, fra un po non ci permette neanche di installare le app.
 

Star-Dust

Expert
Licensed User
Longtime User
C'è un errore di tipo TcuContact dev'essere cuContact e TcuPhone in cuPhone

Ho allegato l'esempio corretto. Mi sono permesso di rinominare l'esempio CONATTI in CONTATTI , spero non ti dispiaccia
 

Attachments

  • CONTATTI.zip
    13 KB · Views: 121
Top