//static AccessState GetCurrentLocationAccess(this IAndroidContext context, bool background, bool fineAccess, bool gpsRequired, bool networkRequired) //{ // var status = context.GetLocationManagerStatus(gpsRequired, networkRequired); // if (status != AccessState.Available) // return status; // if (context.IsMinApiLevel(29) && background) // { // status = context.GetCurrentAccessState(P.AccessBackgroundLocation); // if (status != AccessState.Available) // return status; // } // var next = fineAccess ? P.AccessFineLocation : P.AccessCoarseLocation; // status = context.GetCurrentAccessState(next); // return status; //} public static async Task <AccessState> RequestLocationAccess(this IAndroidContext context, bool background, bool fineAccess, bool gpsRequired, bool networkRequired) { if (!IsLocationEnabled(context, gpsRequired, networkRequired)) { return(AccessState.Disabled); } var locationPerm = fineAccess ? P.AccessFineLocation : P.AccessCoarseLocation; if (!context.IsMinApiLevel(29) || !background) { return(await context.RequestAccess(locationPerm).ToTask()); } var access = await context .RequestPermissions ( P.AccessBackgroundLocation, P.ForegroundService, locationPerm ) .ToTask(); if (!access.IsGranted(locationPerm)) { return(AccessState.Denied); } if (!access.IsGranted(P.AccessBackgroundLocation)) { return(AccessState.Restricted); } return(AccessState.Available); }
public static async Task <AccessState> RequestBackgroundLocationAccess(this IAndroidContext context, LocationPermissionType locType) { if (!context.IsLocationEnabled(false, false)) { return(AccessState.Disabled); } context.AssertBackgroundInManifest(); AccessState status; if (context.IsMinApiLevel(30)) { // Android 11+ need to request background separately // Android 12+ user can decline fine, but allow coarse status = await context.RequestLocationAccess(locType); if (status == AccessState.Available || status == AccessState.Restricted) { var bg = await context.RequestAccess(P.AccessBackgroundLocation).ToTask(); status = bg == AccessState.Available ? status : AccessState.Restricted; } } else if (context.IsMinApiLevel(29)) { // Android 10: Request BG permission with other permissions var perms = new List <string> { P.AccessBackgroundLocation, P.AccessCoarseLocation }; if (locType != LocationPermissionType.Coarse) { perms.Add(P.AccessFineLocation); } var results = await context .RequestPermissions(perms.ToArray()) .ToTask(); status = FromResult(results, locType); if (status == AccessState.Available || status == AccessState.Available) { status = results.IsGranted(P.AccessBackgroundLocation) ? status : AccessState.Restricted; } } else { status = await context.RequestLocationAccess(locType); } return(status); }
public static async Task <AccessState> RequestLocationAccess(this IAndroidContext context, LocationPermissionType locType) { if (!context.IsLocationEnabled(false, false)) { return(AccessState.Disabled); } var perms = new List <string> { P.AccessCoarseLocation }; if (locType != LocationPermissionType.Coarse) { perms.Add(P.AccessFineLocation); } var results = await context.RequestPermissions(perms.ToArray()).ToTask(); var status = FromResult(results, locType); return(status); }