2

I'm looking for a working and not deprecated solution for getting the phone number on any Android phone.

I already tried different methods that I found on StackOverflow, but most of them don't work because the majority reads the phone number from the device info. My device, and many others, doesn't have the number stored in the SIM so when using that method it returns null or empty string.

Is there another way to get the phone number, for example using the Google account you have logged in and checking if there is a number associated with it or from Whatsapp?

Things that I have already tried.

IList<string> numeri = new List<string>();

//RETURNS THE NUMBER ON EMULATOR, NOTHING ON MY DEVICE
TelephonyManager telephonyManager = Application.Context.GetSystemService(TelephonyService) as TelephonyManager;
numeri.Add(telephonyManager.Line1Number);


//RETURNS THE NUMBER ON EMULATOR, NOTHING ON MY DEVICE
SubscriptionManager subscriptionManager = (SubscriptionManager)GetSystemService(TelephonySubscriptionService);
IList<SubscriptionInfo> subsInfoList = subscriptionManager.ActiveSubscriptionInfoList;

foreach (SubscriptionInfo item in subsInfoList) {
    numeri.Add(item.Number);
}


//ACCOUNTS ARRAY IS EMPTY ON EMULATOR AND MY DEVICE
Java.Util.Regex.Pattern pattern = Patterns.Phone;
Account[] accounts = AccountManager.Get(this).GetAccounts();
foreach (Account account in accounts) {
    if (pattern.Matcher(account.Name).Matches()) {
        numeri.Add(account.Name);
    }
} 

I hope somebody can help me with this. Thanks.

EDIT

I've decided to work with the IMEI of the device, for what I need to do, I don't necessarily need the phone number.

TelephonyManager telephonyManager = Application.Context.GetSystemService(TelephonyService) as TelephonyManager;
string IMEIDevice = telephonyManager.Imei;

Edit for removing the possible duplicate message. Thanks to everyone.

Adrian
  • 121
  • 1
  • 9
  • 2
    Prompt the user to enter it, in case they don't want you to have it? – Chris Pickford Jun 20 '19 at 08:30
  • I already thought about asking the user to enter his phone number, but I would like a more automated method. And for the permissions an application asks you if you want to grant that permission, so the user is not obligated to give me his number. Last thing, it's an internal app for my company and the phone numbers will be used to send custom notifications to specific users. – Adrian Jun 20 '19 at 08:59
  • Right now I'm trying to get the phone number from the contacts list since on most phones there is a "Me" contact, but I don't think it will always work because it isn't necessary to set that contact so users could have it blank... – Adrian Jun 20 '19 at 09:06
  • Possible duplicate of [How to get phone number in xamarin forms?](https://stackoverflow.com/questions/55391679/how-to-get-phone-number-in-xamarin-forms) – FreakyAli Jun 20 '19 at 14:17

3 Answers3

1

I've decided to work with the IMEI of the device, for what I need to do, I don't necessarily need the phone number.

TelephonyManager telephonyManager = Application.Context.GetSystemService(TelephonyService) as TelephonyManager;
string IMEIDevice = telephonyManager.Imei;

If some of you guys shows me another way of getting the number, reading the chip of the SIM maybe (?), let me know. Thanks.

Adrian
  • 121
  • 1
  • 9
1

From : Programmatically obtain the phone number of the Android phone

The answer is

Code:

TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

Required Permission:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
Dampen59
  • 513
  • 2
  • 10
  • Yes, I know about this method. The problem is that my device, and other devices, has the saved number, inside the device information, set as "Unknown". So when calling this method it returns a null or empty string, even if I have a SIM with a valid number. – Adrian Jun 20 '19 at 10:16
1

You can try the foollowing code:

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

        String MyPhoneNumber = "0000000000";

        try
        {
            MyPhoneNumber = tMgr.getLine1Number();
        }
        catch (NullPointerException ex)
        {
        }

        if (MyPhoneNumber.equals(""))
        {
            MyPhoneNumber = tMgr.getSubscriberId();
        }

Here is the case:

getLine1Number() Returns blank, not null

Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16
  • Thanks for the suggestion, I decided to go with the IMEI since I want to have the same identifier for every device, and this solution mixes up numbers and IMEIs. It's a clever solution by the way. – Adrian Jun 21 '19 at 15:05