Android Question Get contact list (name and phone number)

biometrics

Active Member
Licensed User
Longtime User
Is it possible to get the contact list with name and phone number on Android. I tried this:


But I'm getting an "java.lang.NullPointerException: null receiver" when calling GetAllPhones.

I'm on Android 10.

Rather than start another thread, is it also possible on iOS?
 

Magma

Expert
Licensed User
Longtime User
You must first get permissions...
B4X:
    For Each permission As String In Array(rp.PERMISSION_READ_CONTACTS,rp.PERMISSION_CALL_PHONE)
        rp.CheckAndRequest(permission)
        Wait For Activity_PermissionResult (permission As String, Result As Boolean)


and then use contactsutils... but in my case needed to use sleep() too (in example pcontacts is.. at globals ... as ... Dim pcontacts As ContactsUtils
xx a long... myphones a string... etc
B4X:
    For Each c As cuContact In pcontacts.FindAllContacts(True)
       Sleep(1) '<---- for some reason if not have it... getting errors at many devices ... i don;t know why...
        For Each phone As cuPhone In pcontacts.GetPhones(c.Id)
            ...
            xx=xx+1
            myphones(xx)=phone.Number
            ...

for iOS cant help you - never used...
 
Last edited:
Upvote 0

biometrics

Active Member
Licensed User
Longtime User
My mistake, did not ask for permission. I'm trying to do so now but I never get the Android permission dialog. Have tried uninstalling the app and reinstalling but never get the popup.

Neither PERMISSION_READ_CONTACTS or PERMISSION_CALL_PHONE are prompting the user.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
... Will be more helpful if you upload sample of code...

But if already gave/not gave permissions you can check at Phone Settings / at the Apps (different at any device) if already disabled the permissions... but as i wrote you before - a way ask for permissions is that.. ofcourse you can check the forum too.
 
Upvote 0

biometrics

Active Member
Licensed User
Longtime User
... Will be more helpful if you upload sample of code...

But if already gave/not gave permissions you can check at Phone Settings / at the Apps (different at any device) if already disabled the permissions... but as i wrote you before - a way ask for permissions is that.. ofcourse you can check the forum too.
I've used it many times so I'm a bit confused why this isn't working. I have uninstalled the app.

B4X:
Sub Process_Globals
    Private rp As RuntimePermissions    
End Sub

Sub Globals
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    rp.CheckAndRequest(rp.PERMISSION_READ_CONTACTS)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        'ok
    Else
        'failed
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
at mine apps always have it like this... but as i said in a loop with other permissions too...
B4X:
        If Result = False Then
            Log(permission)
            ToastMessageShow("Delete/Uninstall App - reinstall and give permissions to work", True)
            Activity.Finish
                'ExitApplication
            Return
        End If

hmmm at Manifest targetting SDK 30 ?....
 
Upvote 0

biometrics

Active Member
Licensed User
Longtime User
at mine apps always have it like this... but as i said in a loop with other permissions too...
B4X:
        If Result = False Then
            Log(permission)
            ToastMessageShow("Delete/Uninstall App - reinstall and give permissions to work", True)
            Activity.Finish
                'ExitApplication
            Return
        End If

hmmm at Manifest targetting SDK 30 ?....
It was on 29 which is the default for new apps in B4A 10.5 (haven't upgraded yet). Tried 30, same thing. How weird. I must be doing something stupid.
 
Upvote 0

biometrics

Active Member
Licensed User
Longtime User
It's a OnePlus 6t with Android 10 if it makes a difference. In Settings/Apps it looks like this.

1625661027485.png
 
Upvote 0

biometrics

Active Member
Licensed User
Longtime User
It took me exactly 3 seconds to see why it doesn't work. It is very simple to find it:

RcbmU69uS0.gif


If the permission is not listed then it will never be approved at runtime.
Thanks, I forgot about that. I added it to the manifest and it shows there now but still not working for me. See attached.
 

Attachments

  • GetContactList.zip
    12 KB · Views: 154
Upvote 0

Magma

Expert
Licensed User
Longtime User
...Hmm did you tried at Emulator (and make calls to it - fake calls into emulator) ----> what is doing ?

OnePlus... may be using different than basic Android OS... someway extended... may be permissions and security of apps are from different app-utility (?) - I am not sure...
Did you check other application-s that for sure need permissions - how it seems in the same "screen" you ve posted (has permissions...) ?
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
For the history my manifest - that works "Reading catalog" (ofcourse have some more permissions)
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="30"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddPermission("android.permission.CALL_PHONE")
AddPermission("android.permission.READ_PHONE_STATE")
AddPermission("android.permission.FOREGROUND_SERVICE")
AddPermission("android.permission.MODIFY_AUDIO_SETTINGS")
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.ANSWER_PHONE_CALLS")
AddPermission("android.permission.READ_CALL_LOG")
AddApplicationText(<activity android:name="anywheresoftware.b4a.objects.preferenceactivity"/>)
'End of default text.
CreateResourceFromFile(Macro, Themes.DarkTheme)

the code requesting permissions:
B4X:
#additionaljar: com.android.support:support-v4


Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

Private rp As RuntimePermissions
....

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
....
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ....
    Dim phone As Phone
    Dim aver As Int

    aver=phone.SdkVersion
    If aver>22 Then
    For Each permission As String In Array(rp.PERMISSION_READ_CONTACTS,rp.PERMISSION_CALL_PHONE,rp.PERMISSION_READ_PHONE_STATE)
        rp.CheckAndRequest(permission)
        Wait For Activity_PermissionResult (permission As String, Result As Boolean)
        If Result = False Then
            Log(permission)
            ToastMessageShow("No permissions - bye bye", True)
            Activity.Finish
                'ExitApplication
            Return
            End If
        Next

Did you try Force Stop (you never know) -> uninstall APP -> Shutdown Phone / Open Phone -> install it again ?
 
Upvote 0

biometrics

Active Member
Licensed User
Longtime User
You did NOT define any Permission in the Manifest. How can Android know you need it?
In the second one I uploaded I did have it but I had it wrong:

B4X:
AddPermission(android.permission.PERMISSION_READ_CONTACTS)

rather than

B4X:
AddPermission(android.permission.READ_CONTACTS)

The request for permission is working now. Now I can move on to the actual task...

Thanks for the help everyone.
 
Upvote 0
Top