Search code examples
androidandroid-2.3-gingerbread

Device Phone Number


I need a way to capture an Android device's phone number in order to send it back to my web api for processing.

I've read about the issues and inconsistencies with the getLine1Number function so my question is this, are there any programmatic alternatives to getLine1Number or is it the only available method to retrieve the device phone number?

To be clear, I am not looking for a UUID, this is a mobile phone application and I require the users phone number for data association.


Solution

  • You can use getLine1Number () for this. It will returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.

    here is an example:

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

    It requires the READ_PHONE_STATE permission to be added on the Androidmanifest.xml file.