Skip to content Skip to sidebar Skip to footer

Getting The Number Of The Phone Xamarin.android?

I found out that this code shared by some users doesn't work in my project. Has anyone a better solution ? TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Co

Solution 1:

Your code is for native Android (Java). Try the below code of Xamarin.Android (c#).

Code:

Android.Telephony.TelephonyManagertMgr= (Android.Telephony.TelephonyManager)this.GetSystemService(Android.Content.Context.TelephonyService);
stringmPhoneNumber= tMgr.Line1Number;

Required Permission:

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

[Edited]

Sometimes Line1Number Property will return null if the number is unavailable, but it does not say when the number might be unavailable.

These are occured due to the reason that the phone number not register in your mobile, or according to some restrictions created by the device manufactures.

From Documentation :

Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.

So you have done everything right, but there is no phone number stored.

If you get null, you could display something to get the user to input the phone number on his/her own.

Solution 2:

Did you add <uses-permission android:name="android.permission.READ_PHONE_STATE"/> to manifest?

It's not really perfect, but the only way (at least to my knowledge). Broader discussion is here Programmatically obtain the phone number of the Android phone

Post a Comment for "Getting The Number Of The Phone Xamarin.android?"