-2

I used this code for fetching the number, But it gave me null

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number(); 
Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42
Mackr
  • 53
  • 1
  • 3
  • Do you want to fetch the existing mobile number? – Sumanth Jois Mar 30 '16 at 03:00
  • It doesn't always return the mobile number, so sometimes it can return null. All is depends of the network provider that provide the mobile number. – HendraWD Mar 30 '16 at 03:14
  • Possible duplicate of [Programmatically obtain the phone number of the Android phone](http://stackoverflow.com/questions/2480288/programmatically-obtain-the-phone-number-of-the-android-phone) – Anirudha Agashe Mar 30 '16 at 05:10

2 Answers2

0

It may not always return mobile number.

Let me explain how this actually works: When you buy a new SIM card for a new number, the provider had already (though not always) put a mobile number in the SIM (SIM Cards do have a little memory). Now TelephonyManager API tries to read that number. If it is available then it returns the number.

Now, you can get a SIM without a mobile number associated with it. Example - new SIM for porting (where you use previous number in new SIM Card). Those SIMs don't have any number stored on them, and hence TelephonyManager returns null.

Now, even if TelephonyManager returns a number, you should not trust it to be right. Because many devices provide option to edit that number.

Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
0

To use getLine1Number() you need either READ_PHONE_STATE OR READ_SMS permission. You may be missing that.

And remember that it returns null if phone number string is unavailable.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68