androidphone-number

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?


Solution

  • We had the same problem in our project. The conclusion was that it depends on the SIM card.

    What happened to us:

    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.