Xamarin环境下锁屏及后台状态无法获取经纬度问题求助
Hey there! Let's work through this background/location-locked latitude/longitude issue in Xamarin — it’s a super common problem, but once you sort out permissions and proper setup, you’ll have it working in no time.
Permissions are almost always the culprit here, and they work differently on iOS and Android, so let’s break them down:
iOS Setup
You need to add specific entries to your Info.plist to request location access, including background permissions:
- Add
NSLocationAlwaysAndWhenInUseUsageDescription: A user-facing explanation of why you need always-on location access (required for iOS 11+). - Add
NSLocationWhenInUseUsageDescription: Explanation for foreground access (fallback, but still needed). - For older iOS versions, you might also need
NSLocationAlwaysUsageDescription.
Additionally, enable the Location updates background mode:
- Open your iOS project properties in Visual Studio.
- Go to the iOS Application tab.
- Under Background Modes, check the box for Location updates.
Android Setup
Permissions vary by Android API level, so cover all bases:
- For Android 9 (API 28) and below: Declare
ACCESS_FINE_LOCATIONandACCESS_COARSE_LOCATIONinAndroidManifest.xml. - For Android 10 (API 29) and above: Add
ACCESS_BACKGROUND_LOCATIONalongside the above two permissions — this is mandatory for background tracking.
Don’t forget runtime permissions! You need to explicitly ask the user for background location access (Android 10+). Here’s a quick snippet using Xamarin.Essentials:
var status = await Permissions.RequestAsync<Permissions.LocationAlways>(); if (status != PermissionStatus.Granted) { // Handle permission denied return; }
Using one-time location requests (like GetLocationAsync) won’t work in the background — you need to set up continuous location updates. Here’s how to do it with Xamarin.Essentials:
// Create a location request with appropriate accuracy and update interval var locationRequest = new GeolocationRequest( GeolocationAccuracy.Medium, TimeSpan.FromMinutes(1)); // Adjust interval based on your needs var cancellationTokenSource = new CancellationTokenSource(); // Start continuous updates — this works in background if permissions are set await Geolocation.StartLocationUpdatesAsync( locationRequest, cancellationTokenSource.Token, (location) => { // Process the updated location here var latitude = location.Latitude; var longitude = location.Longitude; // Save or send this data as needed });
Android Extra: Background Service (Optional)
If you need persistent tracking even when the app is fully in the background, you might want to wrap the location updates in a foreground service. This prevents Android from killing your app’s process to save battery.
Even if your app is configured correctly, the user might have restricted location access at the system level:
- iOS: Go to Settings > Privacy & Security > Location Services > [Your App]. Ensure the permission is set to Always (not "While Using the App" or "Never").
- Android: Go to Settings > Apps > [Your App] > Permissions > Location. Select Allow all time (not "Allow only while using the app").
Both platforms have battery optimization features that can block background location tracking:
- Android: Add your app to the battery optimization exception list. Go to Settings > Battery > Battery Optimization > [Your App] > Don’t optimize.
- iOS: If Low Power Mode is enabled, it might throttle background location updates. Advise users to disable it if consistent tracking is critical.
- Double-check
Info.plist(iOS) andAndroidManifest.xml(Android) for all required permissions. - Confirm you’re using
StartLocationUpdatesAsyncinstead ofGetLocationAsyncfor background tracking. - Verify the user has granted "Always" location access at the system level.
- Exclude your app from battery optimization on Android.
That should cover the main issues! Let me know if you hit any snags along the way.
内容的提问来源于stack exchange,提问作者Nitin Kanere

