TelephonyManager.getLine1Number() failing? - android

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.

Related

Android Mobile Identification Number (MIN)

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).

Retrieving Line1 number from TelephonyManager in 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

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.

Operation on Second SIM Android

I am developing one Android app for that I need to stop and start mobile data.
The problem is that how can I determine whether the mobile is dual SIM or not?
Also if it is dual SIM then how can I do my operation on second SIM?
Thanks !
Safely and decently, you can't.
If you're ready to limit your app to certain devices and give up on it being future proof, you can determine whether the mobile phone is dual SIM by trying to do something that only a specific dual SIM phone can do. If it works, it's surely dual SIM. If it doesn't, well... You won't know. Maybe it's a chipset you didn't cover.
I'll give you an example. If you wish to cover Mediatek chipsets, try this:
long gprsConnectionSimSetting =
Settings.System.getLong(context.getContentResolver(),
"gprs_connection_sim_setting");
It is a Mediatek powered dual SIM phone, if gprsConnectionSimSetting holds one of these values: 0, 1 or 2.
Value 0 stands for "mobile data disabled on both SIMs", value 1 means "mobile data goes through SIM1" and, you've guessed it, value 2 tells us that SIM2 is being used for mobile data.

Get Mobile no from real device

I want to get the mobile number of a sim which is inserted into the device. I know as pr the android SDK TelephoneManger is the class by which we can get it by getLine1Number() method. but it always returns null values.
Queries:
Can i get mobile number from
device?
Is it a sim dependent or in some case
also device dependent?
Any other ways to get the mobile number from device?
You are using correct and the only approach available to the common user (AFAIK). It is mainly SIM dependent (it is not obligatory to have ), and in some cases may be device dependent (getLine1Number() not correctly implemented).
To be more precise, IMSI uniquely identifies SIM. But MSISDN (the subscriber number) is not uniquely related to SIM, as it is possible to change MSISDN on the SIM. Network may use IMSI from SIM to find MSISDN in the HLR and establish the call.
So, MSISDN is not mandatory to be on the SIM.
You should look at this question and its comments.
Some SIM cards seems to cause getLine1Number() to return null... and no one has found a way to deal with this.
More information here
In that case you always save your phone no using SharedPreference by using the method getLine1Number() method. when you install your application then your phone number is saven and when when new sim card is used then send SMS to ur registered number. you have to always check after starting the application that your stored number is same with your new number.if same nothing will be happned ,if not same then ur app will send SMS.

Categories

Resources