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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With