4

I use the TelephonyManager to do this and you can access TelephonyManager s methods as given below

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
String number = telephonyManager.getLine1Number();

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 available.

I have also given application permission to make this query by adding the following to your Manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

The number is always blank or null...

While I have also crosschecked calling and i'm able to call from that number(i.e inserted sim no)

I have gone through numerous documentations but nothing help me out to get the phone number from sim so as to be used in my app..

With Regards,

Arpit Garg

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
Arpit Garg
  • 8,476
  • 6
  • 34
  • 59
  • 2
    You can't reliably get the phone number. Using telephonyManager.getLine1Number() only works if the phone number is present on the SIM card. Some SIM cards don't have the phone number on them. In that case you can't get it. – David Wasser May 16 '12 at 14:02
  • Similar: http://stackoverflow.com/questions/5134398/telephonymanager-getline1number-failing/ – trante Nov 19 '13 at 13:11

2 Answers2

-1

AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators. There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]

But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.

Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.

Cons: 1. you will need SMS_READ permission [sorry for that] 2. You will get all the sim numbers ever used in the device. this problem can be minimized with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.

Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
-6

you should try this

TelephonyManager phoneManager = (TelephonyManager) 
   getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();
Anand Tiwari
  • 1,583
  • 10
  • 22