Location permission issue

Wei Wen 1,096 Reputation points
2021-07-07T18:11:21.207+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,314 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,811 Reputation points Microsoft Vendor
    2021-07-08T02:16:32.967+00:00

    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.


0 additional answers

Sort by: Most helpful