In my application I read device phone number as
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
// Read phone number of device owner.
String phoneNumber = telephonyManager.getLine1Number();
where, this will work till device having phone application.
But when using device is a tablet(some tablet doesn't have phone application), then what will be the value of phoneNumber? Is it will null or blank String? Or it will be an error?
I couldn't check it on emulator because SDK having Samsung Galaxy tab and that having Phone app.
this will work till device having phone application.
No, it will not. It is dependent upon what phone number was programmed into the SIM card. On some phones, this returns null. On my personal phone, it returns the wrong phone number. Do not rely upon getLine1Number() for much of anything.
Is it will null or blank String?
I am reasonably certain that it will return null. It should not raise an Exception.
Related
My android application needs to retrieve the phone number of the device. I'm using the method getLine1Number() to retrieve it, however on some phones it returns a wrong value.
Code:
TelephonyManager tMgr = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
String number = tMgr.getLine1Number();
The phone returning a wrong value is a Samsung Galaxy S6 running android 6.0.1. In the phone settings under Phone number it displays the same wrong value, however under MIN the correct phone number is displayed.
Can anyone give me any ideas or solutions to this problem? I haven't been able to find any way of retrieving the MIN value from an android phone. Nor have I identified why this phone returns a wrong value.
There is no function to get the phone number of a device. Because of the ways some carriers set up SIMs, its actually possible that the device itself doesn't know what its own phone number is. If you actually need a phone number, you need to ask the user for it. If you need a unique id for the phone, other things like ANDROID_ID are more appropriate (remember not ever android device even has a phone number- tablets, watched, and TVs may not).
Some phones return null when I want to get the phone number. In other phones I get the phone number.
This happens when Settings -> About Phone -> Status -> My phone number is "Unkow". Not writed there the phone number.
Why is this happening?How can I get a phone number ?
The code:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
And:
TelephonyManager tMgr = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Your code is perfect to get phone number of Android device.It will return you number.But sometimes it gives you null because that devices Service provider has not given permission to do that.
Means It works like : Android devices Request SIM card/Service provider for card info with phone number. And In response, Some provider have returned phone number of user and some has refused to do so.
Can you clarify what specifically you're asking for? If you're saying that it happens when in your phone's settings, under About Phone->Status->My phone number, it says "Unknown", then unfortunately that's not a programming question and not a question for here. Is that the case?
You cannot get device phone number in any ways. It depends on the service provider, some may allow access and some may not since it is completely dependent on the network access privileges. But in rare case you would get the phone number once the user has configured their own mobile number with the SIM card provider. The other way to do this by fetching the number from Facebook that too the user should have registered their mobile number, whereas the earlier versions of whatsapp provides this feature, but it is not compatible with the newer ones.
Note: It works with few custom phones which provides the access.(i.e., phones synced with only particular network providers)
TelephonyManager manager1 = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String strMobile1 = manager1.getLine1Number();
This is my code for getting mobile number of but its not working. I have also take necessary permission in manifest file
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() only get the number stored on the sim card..a lot of operators don't write there the sim number..on some phones (nokia, blackberry, some olds android, iphone) you can set your own mobile number and the phone will store it on the sim card, at that point getLine1Number() will return the phone number
The method you are using is the only one part of the SDK to do this, and only works on devices where the number is stored on the SIM card, which only some carriers do.
For all other carriers, you will have to ask the user to enter the phone number manually, as the number is simply not stored anywhere on the device from where you can retrieve it.
source here
I want to retreive mobile number of device programatically. My code is:-
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String strMobileNumber = telephonyManager.getLine1Number();
and I have set READ_PHONE_STATE permission in AndroidManifest.xml file. This is working on emulator but on actual device with eclair version, it is returning empty string. Is there any other way to get mobile no?
There is no reliable way to "retreive mobile number of device programatically", AFAIK. getLine1Number() reports the number supplied by the SIM card for GSM phones, and the mobile number does not need to be on the SIM card.
I want to get phone number of android device. I use this code:
TelephonyManager tm = (TelephonyManager)this.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber= tm.getLine1Number();
On my phone (HTC Wildfire) I can not get phone number - all I get is empty string. If I run code on emulatur, phone number is normally showed.
If I go to Settings=>About phone=>Phone identity->Phone number, phone number is "Unknown".
Where is the problem? Is something wrong with my SIM?
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.
Instead of the phone number use the SIM card serial number, which always exists and is always a valid value.
telephonyManager.getSimSerialNumber();
SIM serial number is unique within the operator at least. So combined with
telephonyManager.getNetworkOperator();
you'll have a unique identifier for a subscriber within a country.
This combined with
telephonyManager.getNetworkCountryIso();
you'll have a globally unique identifier for a subscriber.
Hope this will help.
Cheers.
SIM card stores phone number and it can be edited by you. Cant say for Android, but on WM you can easily edit your phone number, which is stored on SIM card.
Of course, it does not mean that you can change your phone number on the fly. Actually, you are free to write there any number you like, and the programs will believe, that this is your real number :)
When I've moved a SIM card into a new phone, I've sometimes found that the phone number does not show up in Settings -> About phone -> Status -> My phone number.
My data points:
AT&T Atrix putting completely new SIM in it says “Voice MSISDN Line1 null”.
T-Mobile G1 putting new SIM card into it correctly displays the phone number.
T-Mobile G2 moving SIM card from my old G1 into is says "Unknown".
Telephone number available if the SIM card is allows it get his.
In our company I found only one SIM, which is allowed get number. I'm tetsting this SIM on all android device. All ok. getLine1Number() is ok.
Now I'm looking for get phone number for other SIM. Have send sms to our sms-server or other android device(with the processing of SMS)
I use a simcard which only permits data transfer. It doesn't permit phone calls.
When I try to get phonenumber with getLine1Number(), I get empty string.