Retrieving Line1 number from TelephonyManager in Android - android

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

Related

The phone return empty when I want to get the phone number

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)

Android: Is there any other way to get the SIM number from the android device other than getLine1number()?

I tried getLine1number(), for some device it gets the number. but for some, it returns empty string("").
As far as I tested, when in the device's Settings > Status > My Phone number is not empty, I get the number otherwise I am not able to get the Phone number.
Is there any other way to get the Phone number other than the above method?
As per my knowledge their is no any way to find a telephone number other than .getLine1Number() method.
Provide you some options that you try:-
A) If number return null means, your phone number not register in your mobile.For that you can use the sim card serial number.It is unique number.
TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
B) 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.
Hope it will helps you.

MSISDN : Is it a SIM Card Data? Why all The Provided Function (from Blackberry and Android) to fetch MSISDN not reliable?

I have several question about MSISDNs. I understand:
MSISDN is the basically the phone number
It is not IMSI
What i need to know further are:
Is MSISDN number burnt (stored) in SIM Card? If it is yes, are all providers make sure that there is MSISDN information in SIM Card? If it is no, to be clarified, None of programming code can fetch the MSISDN number?
Some people suggest to fetch MSISDN as below code. But both the code return null if "My Phone Number" is not set in device. On the other way around, it will return the "My Phone Number" if it is set.
Thus, the question is: "My Phone Number" equals to MSISDN?
TelephonyManager.getLine1Number(); --> for Android
Phone.getDevicePhoneNumber(true); --> for Blackberry
As code above, the return String data is fetched from the device itself or from the SIM Card?
I have some insight into the matter for you.
The MSISDN can be stored on the SIM card, however most network providers (all providers in South Africa) do not store the MSISDN on the SIM card. There are several reasons for this, the most notable being:
Dynamic MSISDN allocation: Prepaid SIMs are sometime allocated an MSISDN when they are first used. This means that the network has a pool of available MSISDNs and allocate them whenever a new prepaid SIM comes online. This means that when the SIMs are shipped they do not have an MSISDN yet, which means you can't store an MSISDN prior to shipping the SIMs. Some networks 'expire' MSISDN allocations after a period of inactivity, especially when their available numbers are running low, and return these numbers to the available pool.
Mobile Number Portability: Some countries allow mobile subscribers to keep their MSISDN but change networks. This means that the MSISDN will be moved from a SIM that is for 1 network to a SIM that is for another network.
So the user often ends up with the MSISDN entry on the SIM being blank. The user can still store the MSISDN on the SIM themselves, but this also allows them to store any arbitrary number, and it is not guaranteed to be set or to even be the subscribers actual phone number.
2. Those calls you asked about fetch the number that is stored in the SIM card. If the number is not set you won't get it, and if the user has the wrong number set then you will have the incorrect details.
3. Correct, this string is fetched from the SIM itself.
The only semi-reliable way I have for detecting MSISDN is via WAP/WEB when the Mobile Operator includes the MSISDN in the headers (you can do some webview trickery to see what headers are coming through) - however you only see these if the network supports it and if the user is on Mobile Data - i.e. It won't work if the user is on WiFi.
The other mechanism I can think of (the only guaranteed mechanism I can think of) is via a USSD session, as you always get the correct MSISDN from the operator, but I haven't found a way of programatically opening a USSD session and reading the response (in Android at least). I have a USSD service whose only output is the MSISDN but unfortunately this hasn't proven usable to me yet.
Since nobody replied in the last hour since you posted your question, here is what I know (although I am not an expert in this area, just have some experience):
MSISDN is the phone number; the number people can call you on.
It cannot be "burnt" into the sim card as you can change sim card (upgrade or changing to other operator) and keep your phone number = MSISDN. I think there is a mapping table at the operators that covert from sim card ID to MSISDN and back when call/data transfer is made. The MSISDN might be stored on the card.
You should take into account that MSISDN has BIGINT type.
Therefore outputting it make type transformation in advance,
like this

how to obtain mobile number of own device in android?

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.

TelephonyManager.getLine1Number() failing?

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.

Categories

Resources