question

WeiWen-3421 avatar image
0 Votes"
WeiWen-3421 asked LeonLu-MSFT commented

Location permission issue

My app needs to access users' locations. In Manifest file, I added the needed permissions and in MainActivity I also added Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults) in OnRequestPermissionsResult.

The first time the app is launched, it will ask the user for permissions. The issue is that even though I selected "Allow only while using the app", when I go to the device's Settings -> Location -> App permissions and find my app, it still has "Deny" selected. I wonder why my initial choice has no effect? Is there a way to set the permission to what I choose the first time I run the app? I don't want the users to the device's Settings to manually change this. That also confuses them since they thought they chose "Allow only while using the app" while it is not the case.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

I notice in your previous thread, You used Geolocation.GetLastKnownLocationAsync(); in the forms.

Please check If you have add following permissions and uses-features in the AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.locationpermissiondemo">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
    <application android:label="LocationPermissionDemo.Android" android:theme="@style/MainTheme"></application>


    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-feature android:name="android.hardware.location" android:required="false" />
    <uses-feature android:name="android.hardware.location.gps" android:required="false" />
    <uses-feature android:name="android.hardware.location.network" android:required="false" />
 
</manifest>


I use following code to get the location in xamarin forms.

private async void Button_Clicked(object sender, EventArgs e)
        {
            try
            {
                var location = await Geolocation.GetLastKnownLocationAsync();

                if (location != null)
                {
                    Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
                }
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                // Handle not supported on device exception
            }
            catch (FeatureNotEnabledException fneEx)
            {
                // Handle not enabled on device exception
            }
            catch (PermissionException pEx)
            {
                // Handle permission exception
            }
            catch (Exception ex)
            {
                // Unable to get location
            }
        }
    }


When I click the "Allow only while using the app", I get the correct permission like following screenshot.

112795-image.png

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (30.3 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LeonLu-MSFT In my last post, I didn't use Geolocation.GetLastKnownLocationAsync(). I was asking a question about Geolocation.GetLocationAsync(), because it didn't seem to be invoked. This is a different question from the last post. In this post, I asked why the location permission was still denied even though the users selected "Allow only while using the app" when they first installed the app.

I can see that you have this permission that I don't have. I will add it to see if this makes any difference:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

0 Votes 0 ·

OK, I used Geolocation.GetLocationAsync() to get location, I can get the location permissions as well. Please use following way to check the applicaions' permission, Open the Settings=> App & notifications =>click your application=> select Permission=>check the allowed.

113171-image.png


0 Votes 0 ·
image.png (195.0 KiB)

@LeonLu-MSFT This is how I manually set the app's permissions. But what I want to achieve is that once the user selects "Allow only while using the app", in the app settings that you showed here the permission will be set likewise. But what happened was that even though I selected "Allow only while using the app" when the app ran for the first time, in App settings it was still "deny" checked.

0 Votes 0 ·
Show more comments