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.
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.
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 theSettings
=>App & notifications
=>click your application=> select `Permission``=>check the allowed.@Leon Lu (Shanghai Wicresoft Co,.Ltd.) 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.
Above way is check the permission's way. I select the "Allow only while using the app" at the runtime permission, with
Geolocation.GetLocationAsync()
, location permission in the allowed. Please create a new project, add my above permissions and uses-features to theAndroidManifest.xml
, then useGeolocation.GetLocationAsync()
in the button click, if you can get the correct result.Sign in to comment