Reputation: 661
I'm writing a test application, on the xamarin for the android. You need to know the phone number. I tried a lot of options. Last:
[assembly: Xamarin.Forms.Dependency(typeof(PhoneNumberService))]
public string GetMyPhoneNumber()
{
TelephonyManager mgr = Application.Context.GetSystemService(Context.TelephonyService) as TelephonyManager;
return mgr.Line1Number;
}
AssemblyInfo.cs:
<`uses-permission android:name="android.permission.READ_PHONE_STATE"/>`
AndroidManifest.xml:
[assembly: UsesPermission(Android.Manifest.Permission.ReadPhoneState)]
On the emulator works well on the phone does not work. Returns an empty string.
How to get a phone number?
Upvotes: 2
Views: 4363
Reputation: 1532
To solve these problems,this link might help you
Try that:
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)GetSystemService(TelephonyService);
var Number = mTelephonyMgr.Line1Number;
Upvotes: 1