TelephonyManager.getLine1Number() failing?
We had the same problem in our project. The conclusion was that it depends on the SIM card.
What happened to us:
- Galaxy S with AT&T SIM card: can read phone number, Settings shows number
- Same Galaxy with an European SIM card: cannot read the number and "unknown" in Settings (cell phone was perfectly functional, just couldn't read the number)
This has been reported in other forums as well.
In the end we decided to ask the user for the phone number. A bit more involved, actually: if( "SIM card present" && "cannot read the cell number") "ask user";
. Otherwise we will keep bugging the user that doesn't a SIM card in the first place.
getLine1Number() is not working
Get SIM Number
TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
Get Phone Number
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
This Will Show you if applicable otherwie show this.
getLine1Number() Returns blank, not null
To get the phone number from the device , first you have to set your own phone number on the device, just through :
Settings -> About Phone -> Status -> My phone Number
Phone numbers are not available on SIM for each operators, like in india Sim dont have phone numbers in any memory, So WE cant get phone number from these connection. However, some countries, and operators have stored phone numbers on SIM, and we can get those. TO make this to work for all devices we can employ two strategies:
To avoid this problem , we can catch the error and work accordingly. Like:
TelephonyManager tMgr = (TelephonyManager)
ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE);
String MyPhoneNumber = "0000000000";
try
{
MyPhoneNumber =tMgr.getLine1Number();
}
catch(NullPointerException ex)
{
}
if(MyPhoneNumber.equals("")){
MyPhoneNumber = tMgr.getSubscriberId();
}
Get own phone number in android (Many issues tested but not worked)?
As you have found, via the many questions and answers I guess you've looked at already, there is no reliable way to get this information:
TelephonyManager.getLine1Number() normally returns null, but on the rare phone that it actually returns something else, the answer cannot be relied upon since it returns the original phone number from the SIM, which due to phone number portability is rarely the phone number that the phone is currently using
The hack to look for the account name used by WhatsApp is an interesting approach, if WhatsApp is installed. Unfortunately this has now stopped working - WhatsApp no longer creates the Android account using the verified phone number as the account name.
Many apps that need this info resort to doing a round trip SMS exchange, since the outgoing SMS will carry the phone number. (Though if you do this, note that many people object to paying the cost of an SMS message, which may well be an international SMS at a premium rate, so will likely give you a bad review in the Play store).
Be aware that although not common in Western markets, there are an increasing number of Dual SIM Android phones (including the new Nokia X), so there is not a 1:1 correspondence between phone and phone number. You also have the problem if your app runs on a tablet that these don't have a phone number anyway.
Phone number is not getting displayed
According to the documentation .getLine1Number()
"Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable. "
Apparently .getLine1Number()
reads this information from SIM card, so if the operator has set the MSISDN field it will return you its value and null if they did not set this field.
In your case probably your SIM card does not have this field populated by operator.
For More Info You can Visit this.
I got this from here.
Related Topics
Noclassdeffounderror For Code in an Java Library on Android
How to Detect Android App Uninstall
Programmatically Switching Off Android Phone
How to Get Current Time from Internet in Android
What Android Tools and Methods Work Best to Find Memory/Resource Leaks
How to Check If Alarmmanager Already Has an Alarm Set
How to Use Disk Caching in Picasso
Android Starting Service At Boot Time , How to Restart Service Class After Device Reboot
Show Compose Sms View in Android
Using Facebook Sdk in Android Studio
Soft Keyboard Open and Close Listener in an Activity in Android
How to Change the Background Color of Action Bar'S Option Menu in Android 4.2
How to Layout Text to Flow Around an Image
How to Read a Text File from the Sd Card in Android
Error Inflating Class Android.Support.Design.Widget.Navigationview
How to Pass a Value from One Activity to Another in Android
Disable Scrolling in All Mobile Devices
Clear the Entire History Stack and Start a New Activity on Android