最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c# - MAUI Android Fine location permission is not asked even though ACESS_FINE_LOCATION is in AndroidManifest.xml - Stack Overfl

matteradmin4PV0评论

I have a MAUI mobile app that needs fine location and background location permissions. I have added all required entries to AndroidManifest.xml including ACCESS_FINE_LOCATION. But when I request LocationWhenInUse in code, the app only asks for approximate location, never for fine location. When I then request LocationAlways, I get a PermissionException saying

You need to declare using the permission: android.permission.ACCESS_FINE_LOCATION in your AndroidManifest.xml

Here you can see the permission added to AndroidManifest and the the app asking for only approximate permission.

I'm using MAUI's own method to ask for permissions:

await Permissions.RequestAsync<Permissions.LocationWhenInUse>()

I tried to get the permissions via PackageInfo:

var context = Android.App.Application.Context;
var pi = context.PackageManager.GetPackageInfo(context.PackageName, Android.Content.PM.PackageInfoFlags.Permissions);
var perms = pi?.RequestedPermissions.ToList();

But here also ACCESS_FINE_LOCATION does not appear in the list, while all other location permissions do appear. This is an app that's being ported from Xamarin to MAUI. In Xamarin I haven't had this issue. I tried to reproduce it in a new MAUI project, but was unable to. I'm using framework net8.0-android, testing on a physical device running Android 14.

Any help is much appreciated.

I have a MAUI mobile app that needs fine location and background location permissions. I have added all required entries to AndroidManifest.xml including ACCESS_FINE_LOCATION. But when I request LocationWhenInUse in code, the app only asks for approximate location, never for fine location. When I then request LocationAlways, I get a PermissionException saying

You need to declare using the permission: android.permission.ACCESS_FINE_LOCATION in your AndroidManifest.xml

Here you can see the permission added to AndroidManifest and the the app asking for only approximate permission.

I'm using MAUI's own method to ask for permissions:

await Permissions.RequestAsync<Permissions.LocationWhenInUse>()

I tried to get the permissions via PackageInfo:

var context = Android.App.Application.Context;
var pi = context.PackageManager.GetPackageInfo(context.PackageName, Android.Content.PM.PackageInfoFlags.Permissions);
var perms = pi?.RequestedPermissions.ToList();

But here also ACCESS_FINE_LOCATION does not appear in the list, while all other location permissions do appear. This is an app that's being ported from Xamarin to MAUI. In Xamarin I haven't had this issue. I tried to reproduce it in a new MAUI project, but was unable to. I'm using framework net8.0-android, testing on a physical device running Android 14.

Any help is much appreciated.

Share Improve this question edited 18 hours ago Kristof Bergé asked 22 hours ago Kristof BergéKristof Bergé 842 bronze badges New contributor Kristof Bergé is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

2 Answers 2

Reset to default 0

Buff, I also had problems with that in a migration to MAUI.

From version 12 onwards you have to first apply for the location in use permission. To request permissions from code, do something like this:

var permiso_location_fine = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();

if (permiso_location_fine == PermissionStatus.Granted) {
    var permiso_uso_background= await Permissions.RequestAsync<Permissions.LocationAlways>();
    if (permiso_uso_background!= PermissionStatus.Granted)  {
        // Do what you need to do
    }
}
else {
   // Do what you need to do
}

In my case I did two more things:

  1. I hand-added the permissions in the AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  1. I forced it to include the AndroidManifest from the csproj:
<ItemGroup>
        <AndroidManifest Include="PlatformsAndroidAndroidManifest.xml"/>
</ItemGroup>
<AndroidUseLegacyPackaging>False</AndroidUseLegacyPackaging>

All in all, this story should show you the permission to ACCESS_FINE_LOCATION in: 
obj/Debug/net8.0-androidandroid/AndroidManifest.xml when compiling.

Excuse my level of English... I hope it helped you.

Good luck!!!!

First, according to your description, the android.permission.ACCESS_FINE_LOCATION didn't work correctly. You can clean and rebuild or restart the visual studio after you added it into the AndroidManifest.xml.

And then, you can try to uninstall the app on the device you tested on or try different devices / emulators.

Finally, you can try to request the permission by the android native code. Put the following code in the MainActivity:

    protected override void OnCreate(Bundle? savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        if(this.CheckSelfPermission(Manifest.Permission.AccessFineLocation) !=Permission.Granted || this.CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != Permission.Granted)
        {
            this.RequestPermissions(new string[] { Manifest.Permission.AccessFineLocation, Manifest.Permission.AccessCoarseLocation }, 0);
        }
    }

For more information, please check the official document about Request location access at runtime.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far