7

I want to get the mobile number of users, which will be defaultly taken as username, when I used the following code to get the mobile number, it returns an empty string

TelephonyManager tMgr = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
            mPhoneNumber = tMgr.getLine1Number();
            String mobNo = mPhoneNumber.replace("+", "");

One reason I found is My phone number is unknown under Settings->About Phone->Status->SIM Status. How to rectify this issue without giving any burden to the users.

enter image description here

ABI
  • 1,536
  • 18
  • 38

2 Answers2

1

There are at least some SIM Cards/Operators out there who don't provide numbers - I have seen it and you have seen it. There is probably nothing you can do about it.

sstn
  • 3,050
  • 19
  • 32
  • 1
    definitely, solution will be there, because this is very basic need of app developers – ABI Mar 16 '15 at 14:56
  • If it is technically not possibly, it doesn't matter if it is "a very basic need of app developers". This functionality is also not present on iOS, at least not for 3rd party apps. Developers there have to live with it as well. – sstn Mar 16 '15 at 15:05
  • 1
    Even (very popular) apps like WhatsApp ask the user to enter/confirm the phone number. – sstn Mar 16 '15 at 15:08
  • I don't find all the mobiles having this issue, there are exceptions, you can see mobile number in moto e. – ABI Mar 17 '15 at 08:50
1

See getLine1Number() documentation. It says if its unavailable, it returns null. So @sstn is partially right. You can't do much about it. As far as I can remember this method gets number which is stored in the SIM. Some providers restrict the access and other don't provide it.

However that being said if you are really really pushing to get phone number from network automatically, you can try one more thing. Most of the network providers provide their own API. The API might require the user to connect to internet through the network data and not wi-fi or any other method. Although different providers in different countries can differ on that. You can potentially recognize the network provider using getNetworkOperator(), then use provider specific API to get the number. However not sure if its free. For eg: In Canada, Telus has its API but it requires you to partner with them. Similarly with Rogers etc.

If you don't want to go through that pain, the last option is to ask the user to enter it. If you are worried about its validity then you can always do one time code SMS verification.

Hope it helps.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124