4

I have read that using getLine1Number() as in the code below can have problems such as

  1. returning null
  2. returning an invalid string
  3. cause nullPointer exception.

Is there any other problems that it can cause?

The biggest issue, which I am not sure of, is, does it return an invalid number (like the number the phone had before) ? This is the most important problem since if it returns a number, can I depend on that number?

Code:

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

Required Permission:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
Kitkat
  • 77
  • 16
Sohan Alam
  • 529
  • 1
  • 6
  • 12

1 Answers1

1

Yes Null is returned when SIM is present and still we cannot read the number. Some SIM cards doesn't allow to do so while others just don't have the number in the SIM.

Quoting TelephonyManager.getLine1Number() failing, Android issue about TelephoneManager and The impossibleness of getting your PhoneNumber in Android and AlertDialogs on Futures blog, it seems that the presence of number within the SIM isn’t required for operation and is actually assigned by the network. If you read the Question's link, in the comments people have suggested the the number in the SIM can be changed by the user from Android's interface. However that might be only possible in older version. Though I've tried changing the number in the SIM( After Android 4.0 versions), it doesn't allow me to do so. Even if someone changes it, the real number is assigned by the network. But, since you are extracting from the SIM storage, I would say its not 100% secure.

This is my 2 cents. Hope it helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124