Example #1
0
        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);
        }
Example #2
0
        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);
        }