1

I am trying to read the phone number of the device using the following code. When phone number is not available I read the subcriber id. It works in some phones and throws NULL pointer exception in some devices. The device log shows I am getting NULL pointer exception in the following line

if(MyPhoneNumber.equals(""))

Please let me know how to make it work in all devices.

TelephonyManager tMgr =(TelephonyManager)ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE); 

String MyPhoneNumber = tMgr.getLine1Number(); 


if(MyPhoneNumber.equals(""))
             MyPhoneNumber = tMgr.getSubscriberId();
Kara
  • 6,115
  • 16
  • 50
  • 57
user1123931
  • 479
  • 1
  • 8
  • 24
  • possible duplicate of [Get phone number problem](http://stackoverflow.com/questions/5134398/get-phone-number-problem) – John Carter Jul 18 '12 at 05:07

5 Answers5

6

Phone numbers are not available on SIM for each operators, like in india Sim dont have phone numbers in any memory, So WE cant get phone number from these connection. However, some countries, and operators have stored phone numbers on SIM, and we can get those. TO make this to work for all devices we can employ two strategies:

  1. TO avoid null pointer exception, we can catch the error and work accordingly. Like:

    TelephonyManager tMgr = (TelephonyManager) 
                     ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE);
    
    String MyPhoneNumber = "0000000000";
    
    try 
    {
        MyPhoneNumber =tMgr.getLine1Number();
    }
    catch(NullPointerException ex)
    {
    }
    
    if(MyPhoneNumber.equals("")) 
        MyPhoneNumber = tMgr.getSubscriberId();
    
  2. Or we can have a SMS Gateway, and whenever we need the phone number, we can send an sms to the gateway, and then deploy a webservice to return the number, the sms gateway receive the message, however this solution is costly.

Ria
  • 10,237
  • 3
  • 33
  • 60
jeet
  • 29,001
  • 6
  • 52
  • 53
  • second option is hectic but it make sense – dharmendra Jul 18 '12 at 10:54
  • May not be good to bring up this old thread but, as of today, where whatsapp is installed in millions of androids, you can try with `AccountManager.getAccounts()`, there might be an account with type = `com.whatsapp` and the value your phone number, I believe this could be your option 3. – Chor Wai Chun May 08 '13 at 03:23
1

In order to get the phone number from the device , first you have to set your own phone number on the device, just go through :

Settings -> About Phone -> Status -> My phone Number

When you call this code:

MyPhoneNumber = tMgr.getLine1Number();

It's the stored number is actually returning.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
spacetime
  • 49
  • 3
1

enter image description here

TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

String number = tm.getLine1Number();

Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
0

Sometimes the phone number is not set in the phone's settings. In these cases, getLine1Number() will return null. You will need to figure out an alternate way of tracking whatever you're tracking.

This is an issue that people run into a lot. I don't program on android much, but ive seen this get asked a lot. Make sure you use the search to check if the question you're asking has been asked before.

Wug
  • 12,956
  • 4
  • 34
  • 54
0

Add manifest READPHONESTATE Permission

May you use

if(MyPhoneNumber.length<0)
             MyPhoneNumber = tMgr.getSubscriberId();
Shalini
  • 1,733
  • 4
  • 17
  • 31