问题
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?
回答1:
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.
回答2:
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.
回答3:
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 :)
回答4:
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".
回答5:
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.
回答6:
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)
来源:https://stackoverflow.com/questions/5134398/telephonymanager-getline1number-failing