-4

I have tried below code and it is returning null value.

Can anyone help where and what i am doing wrong in below code :

TelephonyManager tManager=(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
phoneNumber=tManager.getLine1Number();

Also, I have included permission in Android Manifest file:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Cœur
  • 37,241
  • 25
  • 195
  • 267
kimte
  • 1
  • 1
  • Look at the example. https://github.com/ChiragSavsani/ContactManager – Chirag Savsani Jan 30 '17 at 04:10
  • 1
    Did you give the permission 'READ_PHONE_STATE' – Sandun Harshana Jan 30 '17 at 04:12
  • @SandunHarshana yes I give the permission. there's no way to get phone number ? pls – kimte Jan 30 '17 at 04:25
  • Are you sure you actually have the permission? https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it Should be throwing a `SecurityException` if you don't, so if you have that in a `try-catch`, make sure you're not ignoring any Exceptions. – Mike M. Jan 30 '17 at 04:27

1 Answers1

3

The method you are using is the only one part of the SDK to do this, and only works on devices where the number is stored on the SIM card, which only some carriers do. For all other carriers, you will have to ask the user to enter the phone number manually, as the number is simply not stored anywhere on the device from where you can retrieve it.

Salauddin Gazi
  • 1,497
  • 1
  • 13
  • 18