How can I programmatically get the phone number of the device that is running my android app?
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE); String mPhoneNumber = tMgr.getLine1Number();
According to the highly upvoted comments, there are a few caveats to be aware of. This can return
"" or even
"???????", and it can return a stale phone number that is no longer valid. If you want something that uniquely identifies the device, you should use
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).
I know it is pain, but most likely the best solution is just to ask the user to enter his/her phone number once and store it.