The Xamarin.Forms application I'm working on is trying to prompt the user for location permissions on initial app startup, and then get their GPS position afterwards. iOS works fine, but when it runs on Android the permission prompt appears, the user clicks "allow," and then the CrossGeolocator.Current object says that the IsGeolocationAvailable and IsGeolocationEnabled properties are false. When the user closes and reopens the app, both are enabled. Is there a way for the CrossGeolocator.Current to recognize that it just got location permissions and that it should update?
What I've got so far:
Geolocator Plugin v3.0.4
Android Target v25
ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION set in Manifest
Current activity set in MainActivity's OnCreate:
Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;
OnRequestPermissionsResult handler in MainActivity:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
Prompts user for location permissions and then gets GPS position:
using Plugin.Geolocator;
using Plugin.Geolocator.Abstractions;
using Plugin.Permissions;
using Permissions = Plugin.Permissions.Abstractions;
using Xamarin.Forms;
.
.
.
System.Threading.Tasks.Task.Factory.StartNew(
async () =>
{
await CheckAndEnableGeoLocatorPermissions();
});
if (CurrentGeoLocator.IsGeolocationAvailable
&& CurrentGeoLocator.IsGeolocationEnabled)
{
CurrentGeoLocator.GetPositionAsync(TimeSpan.FromSeconds(Constants.IntervalTime.GetGeoLocatorCurrentPosTimeOut)).ContinueWith(
x =>
{
CurrentPosition = x.Result;
});
}
.
.
.__
private async System.Threading.Tasks.Task CheckAndEnableGeoLocatorPermissions()
{
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permissions.Permission.Location);
if (status != Permissions.PermissionStatus.Granted)
{
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permissions.Permission.Location);
//Best practice to always check that the key exists
if (results.ContainsKey(Permissions.Permission.Location))
{
status = results[Permissions.Permission.Location];
}
}
if (status != Permissions.PermissionStatus.Granted && status != Permissions.PermissionStatus.Unknown)
{
Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.Alert(
Text.Error_LocationPermissionDenied));
}
}
catch (Exception ex)
{
Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.Alert(ex.Message));
}
}