Is it possible to get own phone number?

StuartM

Member
Licensed User
Longtime User
A search suggests that, subject to SIM security, this is possible in Java via:

B4X:
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
  mPhoneNumber = tMgr.getLine1Number();

But is this possible in B4A?

It doesn't matter if it is unreliable, it would just be a "default" to avoid some app users having to type their own number into an app set-up screen.

(Obligatory, sorry 1st post apology goes here :) Got the trial last night and bought today so still learning.)
 

Brad

Active Member
Licensed User
Longtime User
Hi

I am trying to get phone it own number in order to create some activation licensing, I am using these
ToastMessageShow(pi.GetSimSerialNumber,True)
ToastMessageShow(pi.GetDeviceId,True)
ToastMessageShow(pi.GetSubscriberId,True)
ToastMessageShow(pi.GetLine1Number,True)

only lines 1-3 are ok, the line that I need is giving blank result
I need full prove solution to get phone number (since I will base my entire licensing on it)

Shay,

My app uses the phone number for licensing and this works for me
B4X:
Dim Phonenum As PhoneId
dim sPhonenum as string
'
'
'
sPhonenum = phonenum.GetLine1Number
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
If anyone is looking at grabbing the current phone number
make sure you use:

Dim p As PhoneId
lblPhone.text = p.GetLine1Number


and not

Dim p as Phone

I believe the zip file in this thread has the above line
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Just to clarify :

You've used the following code :

Dim pId As PhoneId
Dim sPhonenum As String
sPhonenum = pId.GetLine1Number

Whilst I understand that you've compacted some of the code above, I think it would help to lay it out a little more stepwise, to ensure that it is behaving as you expect it. Sometimes some code elements can take umbrage at the wrong type being passed, but just set it to blank as opposed to telling you this.

and your manifest file has the following intent in it :

android.permission.READ_PHONE_STATE

I'll retry this on my current phone, which is an HTC One X.
 
Upvote 0

AbbasMohammed

Member
Licensed User
Longtime User
Hi eps, kindly, my code is :
B4X:
Sub Globals
Dim myPhone As PhoneId
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim Phonenum As PhoneId
Dim sPhonenum As String
sPhonenum = Phonenum.GetSubscriberId
Msgbox(sPhonenum ,"")
End Sub

and the manifest is :

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
 
Upvote 0

eps

Expert
Licensed User
Longtime User
So where is this : android.permission.READ_PHONE_STATE

In your manifest?

and why aren't you executing this code?

Dim pId As PhoneId
Dim sPhonenum As String
sPhonenum = pId.GetLine1Number
 
Upvote 0

AbbasMohammed

Member
Licensed User
Longtime User
sorry, i send the wrong code, i have used
B4X:
sPhonenum = pId.GetLine1Number
before and i didn't get any thing , regarding manifest file i didn't change it and i shall try your suggestion right now, where to add this line????
android.permission.READ_PHONE_STATE
 
Last edited:
Upvote 0

AbbasMohammed

Member
Licensed User
Longtime User
Dears :
manfist:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
AddPermission (android.permission.READ_PHONE_STATE)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

main:
B4X:
Sub Globals
Dim pId As PhoneId
Dim sPhonenum As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
sPhonenum = pId.GetLine1Number
Msgbox(sPhonenum ,"")
End Sub
Result: Nothing!!!!
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
AbbasMohammed: Go to your project (application) folder\Object\, it is the file "AndroidManifest.xml".
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
People have discussed that a lot, for your information, it is clear that not always getLine1Number() can return the mobile number. It depends a lot on network providers / SIM card. Sometimes, the number is not burned on your sim card. Thus, for whom who want to use (own) phone number instead of IMEI, retrieving the mobile number from phone is somehow not always reliable... Anyway, Erel is correct when he says so far it is the only way we can use to get the phone number. But in some devices (depending on network providers) it can return Null.

There is no guaranteed solution to this problem because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. This is especially true in some countries which requires physical address verification, with number assignment only happening afterwards. Phone number assignment happens on the network - and can be changed without changing the SIM card or device (e.g. this is how porting is supported).

You can find alternative solutions from here:
http://stackoverflow.com/questions/2480288/get-phone-number-in-android-sdk
or here:
http://stackoverflow.com/questions/...ile-number-of-current-sim-card-in-real-device

For example, you can:
1) ask the user to enter his / her own phone number; or
2) you can use GetSimSerialNumber instead. Sim Serial number is always available and always unique;
 
Last edited:
Upvote 0

eps

Expert
Licensed User
Longtime User
People have discussed that a lot, for your information, it is clear that not always getLine1Number() can return the mobile number. It depends a lot on network providers / SIM card. Sometimes, the number is not burned on your sim card. Thus, for whom who want to use (own) phone number instead of IMEI, retrieving the mobile number from phone is somehow not always reliable... Anyway, Erel is correct when he says so far it is the only way we can use to get the phone number. But in some devices (depending on network providers) it can return Null.



You can find alternative solutions from here:
http://stackoverflow.com/questions/2480288/get-phone-number-in-android-sdk
or here:
http://stackoverflow.com/questions/...ile-number-of-current-sim-card-in-real-device

For example, you can:
1) ask the user to enter his / her own phone number; or
2) you can use GetSimSerialNumber instead. Sim Serial number is always available and always unique;

Erm, for your alternative solutions, the first one, is in fact, the solution that we're using here...

EDIT : Actually both of them use the same solution..!

There may be a way to use a Reflection call to retrieve the phone number... Maybe use both as a fallback?
 
Upvote 0
Top