-1

I am trying to get the phone number of the current phone, and I've used this code (I already have permissions in the manifest):

TelephonyManager mTelephonyMgr;    
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);     

String phoneNumber = mTelephonyMgr.getLine1Number(); 

I tried this code in the android emulator and it works fine and gives me a number (I guess it's the emulator's phone number),

However when I tried this code on my phone(galaxy s2) it returned an empty string.

Thanks

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
Adir Rahamim
  • 453
  • 1
  • 12
  • 31

2 Answers2

1

getLine1Number() is not guaranteed to return a phone number. It only works if the SIM card in your device has the number stored on it.

You can check if it does by going to Settings -> About -> Status and seeing if the phone number is available there. If it isn't, then your carrier doesn't keep the number on the SIM card

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
0

Source : Reading device phone number throws NULLPointerException

In India some operator, this phone number returned is which is stored on the SIM card's MSISDN, which isn't filled by some operators. So it return empty try it with other SIM.

The documentation for getLine1Number() says this method will return null if the number is "unavailable", but it does not say when the number might be unavailable.

Community
  • 1
  • 1
Ajay S
  • 48,003
  • 27
  • 91
  • 111