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.