I am working on xamarin.forms. I need to get the phone number, if the device is dual sim then I need to get both the numbers. How do I achieve this? There so many questions related to this after referring all I came here.
I created one interface in shard project and implemented the interface in an android project. android.permission.READ_PHONE_NUMBERS
created interface in shared project
public interface IDeviceInfo
{
string GetMyPhoneNumber();
}
Implemented the interface in android project
public string GetMyPhoneNumber()
{
try
{
TelephonyManager mgr =
Android.App.Application.Context.GetSystemService(Context.TelephonyService) as TelephonyManager;
return mgr.Line1Number;
}
catch (Exception ex)
{
throw;
}
}
On one of Button click on shared project
private void GetPhone_Clicked(object sender, EventArgs e)
{
try
{
var data = DependencyService.Get<IDeviceInfo>().GetMyPhoneNumber();
}
catch (Exception ex)
{
throw;
}
}
When I am using android 5 it is returning the empty string and if I am using android 6+ it is giving permission error like Java.Lang.SecurityException: getLine1NumberForDisplay: Neither user 10286 nor current process hasandroid.permission.READ_PHONE_STATE, android.permission.READ_SMS, or android.permission.READ_PHONE_NUMBERS
How do resolve this in all android version?