Android Mobile Tracking SDK
The Amazon Location mobile tracking SDK provides utilities which help easily authenticate, capture device positions, and send position updates to Amazon Location Trackers. The SDK supports local filtering of location updates with configurable update intervals. This reduces data costs and optimizes intermittent connectivity for your Android applications.
The Android tracking SDK is available on GitHub: Amazon Location Mobile Tracking SDK for Android
This section covers the following topics for the Amazon Location mobile tracking Android SDK:
Installation
To install the SDK, add the following lines to the dependencies section of your build.gradle file in Android Studio:
implementation("software.amazon.location:tracking:0.0.1") implementation("software.amazon.location:auth:0.0.1") implementation("com.amazonaws:aws-android-sdk-location:2.72.0")
Usage
This procedure shows you how to use the SDK to authenticate and create the
LocationTracker object:
Note
This procedure assumes you have imported the library mentioned in the Installation section.
-
Import the following classes in your code:
import software.amazon.location.tracking.LocationTracker import software.amazon.location.tracking.config.LocationTrackerConfig import software.amazon.location.tracking.util.TrackingSdkLogLevel import com.amazonaws.services.geo.AmazonLocationClient import software.amazon.location.auth.AuthHelper import software.amazon.location.auth.LocationCredentialsProvider -
Next create an
AuthHelper, since theLocationCredentialsProviderparameter is required for creating aLocationTrackerobject:// Create an authentication helper using credentials from Cognito val authHelper = AuthHelper(applicationContext) val locationCredentialsProvider : LocationCredentialsProvider = authHelper.authenticateWithCognitoIdentityPool("My-Cognito-Identity-Pool-Id") -
Now, use the
LocationCredentialsProviderandLocationTrackerConfigto create aLocationTrackerobject:val config = LocationTrackerConfig( trackerName = "MY-TRACKER-NAME", logLevel = TrackingSdkLogLevel.DEBUG, accuracy = Priority.PRIORITY_HIGH_ACCURACY, latency = 1000, frequency = 5000, waitForAccurateLocation = false, minUpdateIntervalMillis = 5000, ) locationTracker = LocationTracker( applicationContext, locationCredentialsProvider, config, )
Filters
The Amazon Location mobile tracking Android SDK has three inbuilt location filters.
-
TimeLocationFilter: Filters the current location to be uploaded based on a defined time interval. -
DistanceLocationFilter: Filters location updates based on a specified distance threshold. -
AccuracyLocationFilter: Filters location updates by comparing the distance moved since the last update with the current location's accuracy.
This example adds filters in the LocationTracker at the
creation time:
val config = LocationTrackerConfig( trackerName = "MY-TRACKER-NAME", logLevel = TrackingSdkLogLevel.DEBUG, accuracy = Priority.PRIORITY_HIGH_ACCURACY, latency = 1000, frequency = 5000, waitForAccurateLocation = false, minUpdateIntervalMillis = 5000, locationFilters = mutableListOf(TimeLocationFilter(), DistanceLocationFilter(), AccuracyLocationFilter()) ) locationTracker = LocationTracker( applicationContext, locationCredentialsProvider, config, )
This example enables and disables filter at runtime with
LocationTracker:
// To enable the filter locationTracker?.enableFilter(TimeLocationFilter()) // To disable the filter locationTracker?.disableFilter(TimeLocationFilter())
Android Mobile SDK tracking functions
The Amazon Location mobile tracking SDK for Android includes the following functions:
-
Class:
LocationTrackerconstructor(context: Context,locationCredentialsProvider: LocationCredentialsProvider,trackerName: String), orconstructor(context: Context,locationCredentialsProvider: LocationCredentialsProvider,clientConfig: LocationTrackerConfig)This is an initializer function to create a
LocationTrackerobject. It requires instances ofLocationCredentialsProvider,trackerNameand optionally an instance ofLocationTrackingConfig. If the config is not provided it will be initialized with default values. -
Class:
LocationTrackerstart(locationTrackingCallback: LocationTrackingCallback)Starts the process of accessing the user's location and sending it to an Amazon Location tracker.
-
Class:
LocationTrackerisTrackingInForeground()Checks if location tracking is currently in progress.
-
Class:
LocationTrackerstop()Stops the process of tracking the user's location.
-
Class:
LocationTrackerstartTracking()Starts the process of accessing the user's location and sending it to the AWS tracker.
-
Class:
LocationTrackerstartBackground(mode: BackgroundTrackingMode, serviceCallback: ServiceCallback)Starts the process of accessing the user's location and sending it to the AWS tracker while the application is in the background. BackgroundTrackingMode has the following options:
-
ACTIVE_TRACKING: This option actively tracks a user's location updates. -
BATTERY_SAVER_TRACKING: This option tracks user's location updates every 15 minutes.
-
-
Class:
LocationTrackerstopBackgroundService()Stops the process of accessing the user's location and sending it to the AWS tracker while the application is in the background.
-
Class:
LocationTrackergetTrackerDeviceLocation()Retrieves the device location from Amazon Location services.
-
Class:
LocationTrackergetDeviceLocation(locationTrackingCallback: LocationTrackingCallback?)Retrieves the current device location from the fused location provider client and uploads it to Amazon Location tracker.
-
Class:
LocationTrackeruploadLocationUpdates(locationTrackingCallback: LocationTrackingCallback?)Uploads the device location to Amazon Location services after filtering based on the configured location filters.
-
Class:
LocationTrackerenableFilter(filter: LocationFilter)Enables a particular location filter.
-
Class:
LocationTrackercheckFilterIsExistsAndUpdateValue(filter: LocationFilter)Disable particular location filter.
-
Class:
LocationTrackerConfigLocationTrackerConfig( // Required var trackerName: String, // Optional var locationFilters: MutableList = mutableListOf( TimeLocationFilter(), DistanceLocationFilter(), ), var logLevel: TrackingSdkLogLevel = TrackingSdkLogLevel.DEBUG, var accuracy: Int = Priority.PRIORITY_HIGH_ACCURACY, var latency: Long = 1000, var frequency: Long = 1500, var waitForAccurateLocation: Boolean = false, var minUpdateIntervalMillis: Long = 1000, var persistentNotificationConfig: NotificationConfig = NotificationConfig())This initializes the
LocationTrackerConfigwith user-defined parameter values. If a parameter value is not provided, it will be set to a default value. -
Class:
LocationFiltershouldUpload(currentLocation: LocationEntry, previousLocation: LocationEntry?): BooleanThe
LocationFilteris a protocol that users can implement for their custom filter implementation. You need to implement theshouldUploadfunction to compare previous and current location and return if the current location should be uploaded.
Examples
The following code sample shows the mobile tracking SDK functionality.
This example uses the LocationTracker to start and stop
tracking in background:
// For starting the location tracking locationTracker?.startBackground( BackgroundTrackingMode.ACTIVE_TRACKING, object : ServiceCallback { override fun serviceStopped() { if (selectedTrackingMode == BackgroundTrackingMode.ACTIVE_TRACKING) { isLocationTrackingBackgroundActive = false } else { isLocationTrackingBatteryOptimizeActive = false } } }, ) // For stopping the location tracking locationTracker?.stopBackgroundService()