Hi
I have been searching for a while now but have not found a solution yet.
I need to request the READ_PHONE_STATE permission.
I have implemented a device specific implementation of my PCL interface IDeviceSettings.
public void RequestPermissions()
{
try
{
if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(Forms.Context, Manifest.Permission.Camera) != (int)Permission.Granted)
{
ActivityCompat.RequestPermissions((Activity)Forms.Context, new String[] { Manifest.Permission.ReadPhoneState }, 0);
}
else
{
// Permission has already been accepted previously
}
}
catch (Exception ex)
{
int x = 1 + 1;
}
}
I also implemented the device specific override to check if a permission was granted or not.
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
switch (requestCode)
{
case 0:
{
if (grantResults[0] == Permission.Granted)
{
//Permission granted
int x = 1 + 1;
}
else
{
//Permission Denied :(
//Disabling location functionality
int x = 1 + 1;
}
}
break;
}
}
I then call this from my codebehind xaml view (for quick testing).
private async void Button_Clicked(object sender, EventArgs e)
{
IDeviceSettings deviceSettings = DependencyService.Get();
bool enabled = deviceSettings.AutomaticTimeEnabled();
await DisplayAlert("Output", enabled.ToString(), "OK");
deviceSettings.RequestPermissions();
//string hwid = deviceSettings.GetHardwareId();
//await DisplayAlert("Output", hwid, "OK");
}
Now i dont get a dialog where i can accept the requested permissions, but the override in MainActivity gets called immediately with Permissions denied.
I also tried launching an activity from android and requesting them there but then i dont even get the callback.
Some more info :
Visual Studio 2017 Community
Minimum Android Version : 4.1 (16)
Target Android Version : 7.1 (25)
Xamarin 4.5.0.486 (fec6f88)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.
Xamarin.Android SDK 7.3.1.2 (9dbc4c5)
Xamarin.Android Reference Assemblies and MSBuild support.
Xamarin.iOS and Xamarin.Mac SDK 10.10.0.37 (ad35de4)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
Using the Plugin.Permissions from James is not an option since it breaks my app, issue can be found here :
https://github.com/jamesmontemagno/PermissionsPlugin/issues/57#issuecomment-318839270
Any help = greatly appreciated.