Android Question Get phone number

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Hi
Is there a way to get the phone number ?
I need the phone number from the device my app is running on.
Need to be able to do it on latest sdk
A sample will be highly appreciated

Thanks
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
i'll try to explain my need - i need to get the phone number
it has to running on any android version including 13 and latest SDK
older versions or SDK are not relevant

so is there a way to get the phone number from latest android & sdk versions?
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
As Erel said, it is no longer possible to access the phone number
SDK33 - how to get unique number of phone | B4X Programming Forum
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
As Erel said, it is no longer possible to access the phone number
SDK33 - how to get unique number of phone | B4X Programming Forum
thank you
so this is the end for getting the phone number
any work around ?
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi
Is there a way to get the phone number ?
I need the phone number from the device my app is running on.
Need to be able to do it on latest sdk
A sample will be highly appreciated

Thanks
Just in case - I have 2 phones - Samsung which is currently in service and LG that is no longer in service. I used LG Android 10 before I upgraded to Samnsung S23 with Android 13.

Both of them show the same phone number even though LG is no longer in service with the cellphone provider. So it's not unique.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
Steps to get device's phone number:
Step 1: Grant runtime permission
Manefest:
'''''''''''''''''''''''''
''get device phone number
AddPermission(android.permission.READ_PHONE_NUMBERS)
'''''''''''''''''''''''''

Step 2: run the code below
B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    '1 get device phone number, if any
    rp.CheckAndRequest("android.permission.READ_PHONE_NUMBERS")
    Wait For B4XPage_PermissionResult (Permission As String, blnResult As Boolean)
    If blnResult=False Then
        ToastMessageShow($"You need to grant Phone permission for the app to function correctly"$, True)
        Return
    End If
    
    Try
        Dim pi As PhoneId
        Dim cell As String=pi.GetLine1Number
        LogColor("phone #=" & cell, Colors.Blue)
        If cell.Length>0 Then
            ToastMessageShow("phone #=" & cell, True)
        Else
            ToastMessageShow("Unable to get phone #", True)
        End If     
    Catch
        Log(LastException)
    End Try
End Sub

Sample app attached
 

Attachments

  • getTel.zip
    14.3 KB · Views: 144
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Steps to get device's phone number:
Step 1: Grant runtime permission
Manefest:
'''''''''''''''''''''''''
''get device phone number
AddPermission(android.permission.READ_PHONE_NUMBERS)
'''''''''''''''''''''''''

Step 2: run the code below
B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    '1 get device phone number, if any
    rp.CheckAndRequest("android.permission.READ_PHONE_NUMBERS")
    Wait For B4XPage_PermissionResult (Permission As String, blnResult As Boolean)
    If blnResult=False Then
        ToastMessageShow($"You need to grant Phone permission for the app to function correctly"$, True)
        Return
    End If
  
    Try
        Dim pi As PhoneId
        Dim cell As String=pi.GetLine1Number
        LogColor("phone #=" & cell, Colors.Blue)
        If cell.Length>0 Then
            ToastMessageShow("phone #=" & cell, True)
        Else
            ToastMessageShow("Unable to get phone #", True)
        End If   
    Catch
        Log(LastException)
    End Try
End Sub

Sample app attached
sorry, but this sample did not work
i tried it on Samsung S22 Ultra - Android 13
got empty string for phone number
Steps to get device's phone number:
Step 1: Grant runtime permission
Manefest:
'''''''''''''''''''''''''
''get device phone number
AddPermission(android.permission.READ_PHONE_NUMBERS)
'''''''''''''''''''''''''

Step 2: run the code below
B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    '1 get device phone number, if any
    rp.CheckAndRequest("android.permission.READ_PHONE_NUMBERS")
    Wait For B4XPage_PermissionResult (Permission As String, blnResult As Boolean)
    If blnResult=False Then
        ToastMessageShow($"You need to grant Phone permission for the app to function correctly"$, True)
        Return
    End If
   
    Try
        Dim pi As PhoneId
        Dim cell As String=pi.GetLine1Number
        LogColor("phone #=" & cell, Colors.Blue)
        If cell.Length>0 Then
            ToastMessageShow("phone #=" & cell, True)
        Else
            ToastMessageShow("Unable to get phone #", True)
        End If    
    Catch
        Log(LastException)
    End Try
End Sub

Sample app attached
sorry, but this sample did not work
i tried it on Samsung S22 Ultra - Android 13
got empty string for phone number
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You may try this too.
Don´t know if it´s still work or not though.
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
As written in https://www.b4x.com/android/forum/threads/sdk33-how-to-get-unique-number-of-phone.146991/post-931661
it is not possible to get the Phonenumber in Android 10+.
Even if there are some example they may not work. You just find out for yourself.

Ask the user for their phonenumber.
Send the number to your server.
Send a SMS with a Authentificationcode to the number from your server.
Let the user enter the code to verify it.
hi & thanks for your input
i got this but these friends said their sample works on latest android & SDK so i hopped...
using the ask for phone & verify by sms is a known "trick" but not really suitable for my needs
so i'll have to think of alternatives unless someone will come with a working solution / work arround
 
Upvote 0
Top