Android Question revisiting question: any way to get the phone number?

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
hi

i know it was asked million times but still this is an open question
i know apps in play store some how manage to read the phone number of the device
is there any way to do this ?
how?

a small basic sample will be highly appreciated

thanks
 

peacemaker

Expert
Licensed User
Longtime User
Are you sure that getting user's private data will not be violated ?
IMHO all these methods will not work in all law-abiding devices.
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Have you tried PhoneId.GetLine1Number? Make sure to request the READ_PHONE_STATE permission at runtime.
yes i did and got errors
checked and rechecked
i'm not looking for imei now - only phone number
and i did see other app do that...

EDIT:
checked on older phone - works
older version of android is ok
on my Samsung S23U - android 14 i get error on GetLine1Number saying i don't have permission

nothing changed only replaced the phone
 
Last edited:
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Post the relevant code and the full error message.

ok

attached is the sample app
and this is the error i get (only on android 14. on old versions is works)

thanks for your help




Error occurred on line: 67 (Main)
java.lang.SecurityException: getLine1NumberForDisplay: Neither user 10782 nor current process has android.permission.READ_PHONE_STATE, android.permission.READ_SMS, or android.permission.READ_PHONE_NUMBERS
at android.os.Parcel.createExceptionOrNull(Parcel.java:3069)
at android.os.Parcel.createException(Parcel.java:3053)
at android.os.Parcel.readException(Parcel.java:3036)
at android.os.Parcel.readException(Parcel.java:2978)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getLine1NumberForDisplay(ITelephony.java:10978)
at android.telephony.TelephonyManager.getLine1Number(TelephonyManager.java:5919)
at android.telephony.TelephonyManager.getLine1Number(TelephonyManager.java:5887)
at anywheresoftware.b4a.phone.Phone$PhoneId.GetLine1Number(Phone.java:488)
at GetPhoneInfo.Sample.main$ResumableSub_GetPhoneID.resume(main.java:524)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:150)
at anywheresoftware.b4a.BA$2.run(BA.java:395)
at anywheresoftware.b4a.BA.setActivityPaused(BA.java:467)
at GetPhoneInfo.Sample.main$ResumeMessage.run(main.java:313)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:230)
at android.os.Looper.loop(Looper.java:319)
at android.app.ActivityThread.main(ActivityThread.java:8893)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
** Activity (main) Resume **
 

Attachments

  • GetPhoneNum.zip
    9.4 KB · Views: 65
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
thanks
did that
now i get no errors but also no phone number - it comes back as empty string
so it solved the errors but not the phone number
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
B4X:
#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
End Sub

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

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume
    Try
        Dim ph As PhoneId
        
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_PHONE_STATE)
        
        Starter.rp.CheckAndRequest("android.permission.READ_PHONE_NUMBERS")
        Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            LogColor("phone #=" & ph.GetLine1Number, Colors.red)
        End If
        
    Catch
        Log(LastException)
    End Try
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Try
        
        Dim pi As PhoneId
        Log(pi.GetLine1Number)
    
        xui.MsgboxAsync("Phone number " & pi.GetLine1Number, "B4X")
        
        
        
        
    Catch
        Log(LastException)
    End Try
    
End Sub

Tested on my Adroid 14 phone
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
i don't know what i'm missing but it doesn't work
tried your sample - no phone number
attached is my sample project to test that
you will be able to see all my attempts as i commented them
nothing works
i try on samsung s23 ultra android 14 ...

i will appreciate it if you could look into the code and spot my mistake

thanks
 

Attachments

  • GetPhoneNum.zip
    9.7 KB · Views: 84
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Just think why all apps with the user registration ask user to input the phone number himself.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
tested on my Samsung SM-916U Android 14 - works fine. It shows my phone number.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
also what are you trying to achive is doesn't make sence - if you want to use a phone number as an ID it's useless.

I have 5 Android phones. One that I'm using now and 4 that I used before. All of them shows the same phone number even though they are no longer connected to the network but keeping the SIM card.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
check if your phone in Developer mode. Not sure if it's related but why not?

My Samsung with Adroid 14 is not, my old LG is in - works on both of my phones
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
check if your phone in Developer mode. Not sure if it's related but why not?

My Samsung with Adroid 14 is not, my old LG is in - works on both of my phones
yes, my phone is in developer mode
i don't think it has anything to do with that, and as you said - one of your phones is with and the other without
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…