Skip to content

Latest commit

 

History

History

BackgroundLocationTracking

Background Location Tracking

BackgroundLocationTracking is a .NET MAUI application designed to track the location of a device in the background using the ArcGIS Runtime SDK for .NET.

Android application uses Foreground Service to track the location in the background. iOS application uses SystemLocationDataSource.AllowBackgroundLocationUpdates to track the location in the background.

Screenshot

Important Configurations

Android

MainPage.xaml.cs

Android requires the use of a ForegroundService to track location in the background. Ensure the following code is added to the MainPage.xaml.cs file to configure the location data source:

_locationDataSource = new SystemLocationDataSource();

To start the ForegroundService on current application context, use the following code:

var intent = new Android.Content.Intent(Android.App.Application.Context, typeof(LocationService));

// Foreground Services are only supported and required after Android version Oreo (API level 26)
// Foreground service is required to keep the service running in the background when the main app is not in the foreground.
// Start the service as a foreground service.
_ = Android.App.Application.Context.StartForegroundService(intent);

AndroidManifest.xml

Ensure the following permissions and service declaration are added:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<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" />
<application>
    <service android:name=".LocationService" android:foregroundServiceType="location" />
</application>

LocationService.cs

Define the Android foreground service for location tracking:

[Service(ForegroundServiceType = Android.Content.PM.ForegroundService.TypeLocation)]
public class LocationService : Service
{
    public override StartCommandResult OnStartCommand(Intent? intent, StartCommandFlags flags, int startId)
    {
        var notification = GetServiceStartedNotification(this);
        StartForeground(1, notification, Android.Content.PM.ForegroundService.TypeLocation);
        return base.OnStartCommand(intent, flags, startId);
    }
}

iOS

MainPage.xaml.cs

Ensure the following code is added to the MainPage.xaml.cs file to configure the location data source and request background location updates:

_locationDataSource = new SystemLocationDataSource
{
    // Set AllowsBackgroundLocationUpdates to true to allow location updates when the app is in the background.
    AllowsBackgroundLocationUpdates = true,

    // Set ActivityType which is used to determine when location updates should be delivered.
    // This is used to help determine when to turn off GPS hardware to save power.
    ActivityType = CoreLocation.CLActivityType.Other,
};

Info.plist

Add location usage descriptions and background modes:

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app requires background location updates for tracking your position.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>