30

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?

John Carter
  • 53,924
  • 26
  • 111
  • 144
knagode
  • 5,816
  • 5
  • 49
  • 65
  • 2
    On your actual phone it says that your phone number is "Unknown"? – Mohit Deshpande Feb 27 '11 at 16:51
  • 5
    I'm gonna get groans for this, but doesn't just about everyone at SO have a problem getting phone numbers? :D Sorry, had to be done. But I dont think its a problem with your sim, your service provider may not have specified the number on your phone during activation. – Sumit Feb 27 '11 at 16:56

6 Answers6

36

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.

Christian Garbin
  • 2,512
  • 1
  • 23
  • 31
  • 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. – andQlimax Dec 09 '13 at 15:19
  • @andQlimax How to do that? – Shobhit Puri Dec 13 '13 at 18:27
  • @ShobhitPuri storing the number on the sim depends from the phone and from the android version on it..On some android phone there is an option under "call settings->additional settings->your number" there you can set the number..you can't do it by calling an API, it is a system level option included on the phone rom – andQlimax Dec 16 '13 at 09:41
  • @andQlimax Thanks. Atleast in Android 4.0 above I was not able to change it. On which version of Android have you tested it ? – Shobhit Puri Jan 09 '14 at 04:21
  • @ShobhitPuri depends also from the phone, on the galaxy nexus on 4.x there was, on the nexus 5 there isn't.. – andQlimax Jan 09 '14 at 12:48
11

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.

Nar Gar
  • 2,591
  • 2
  • 25
  • 28
  • 1
    What if the user doesn't have a SIM card or has multiple SIM cards? – Alex Bitek Aug 24 '15 at 12:56
  • Then obviously this method won't work. You need to look for a different unique identifier for your app if you intend to make an app for devices that for some reason don't have a SIM (such as majority of tablets). TelephonyManager does cater for multiple SIMs – Nar Gar Aug 26 '15 at 02:15
5

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

trashkalmar
  • 883
  • 5
  • 11
  • 1
    @trashkalmar: How can one do that? Is it possible in non-rooted phones as well? I presume it has nothing to do with rooting of a phone? – Shobhit Puri Dec 13 '13 at 18:49
  • 1
    I'm unsure that its possible on Android, but I`ve done this on Windows Mobile. Maybe, some AT command exists. – trashkalmar Dec 16 '13 at 17:16
2

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".
Dan J
  • 25,433
  • 17
  • 100
  • 173
1

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)

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
RN3KK Nick
  • 701
  • 6
  • 12
0

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.

trante
  • 33,518
  • 47
  • 192
  • 272