Install SDK
The best way to add the SDK to your project is via Gradle in Android Studio.For details on the latest SDK releases, see the releases page on GitHub. You can also star ⭐️ and watch 👀 the repo.
Gradle (recommended)
The SDK is distributed using Maven Central. Add the SDK to thedependencies section of your app’s build.gradle file:
Add manually
You can also add the SDK to your project manually. Download the current release and unzip the package. The package contains anaar file. In Android Studio, add the SDK as a module using File > New Module > Import .JAR/.AAR Package.
Dependencies
The SDK depends on Google Play Services Location version21.0.1 and higher, which will be automatically included as a transitive dependency by Gradle. Learn more about managing dependencies in Gradle here.
If you haven’t already configured your project for Google Play Services, follow the instructions here.
As of version 3.8.0, the SDK uses play-services-location:21.0.1. If another dependency that depends on play-services-location is causing version conflicts, you can exclude the transitive dependencies and include the play-services-location dependency at the project level instead. We strongly recommend using 21.0.1 or higher.
As an alternative to Google Play Services, the SDK also optionally supports Huawei Mobile Services.
The SDK currently supports API level 16 or above.
Initialize SDK
When your app starts, in applicationonCreate(), initialize the SDK with your publishable API key, found on the Settings page.
Use your Test Publishable key for testing and non-production environments. Use your Live Publishable key for production environments.
Network timeout
To override the default network timeout, setnetworkTimeout on RadarInitializeOptions as a Kotlin Duration. The extended timeout used for longer-running requests is automatically set to 2.5x this value. Values are clamped to [1, 300] seconds.
Kotlin
Request permissions
Radar respects standard Android location permissions. For foreground tracking or trip tracking with continuous mode, Radar requires theACCESS_FINE_LOCATION permission for precise location and/or the ACCESS_COARSE_LOCATION permission for approximate location. These permissions are automatically added by the SDK manifest along with the INTERNET, ACCESS_NETWORK_STATE, FOREGROUND_SERVICE, and RECEIVE_BOOT_COMPLETED permissions.
For background tracking or geofencing with responsive mode, and if targeting API level 29 or above, Radar also requires the new ACCESS_BACKGROUND_LOCATION permission. You must add the ACCESS_BACKGROUND_LOCATION permission to your manifest manually. To start a foreground service or use the continuous tracking preset, you must add the FOREGROUND_SERVICE_LOCATION permission:
Build and run the app to make sure permissions prompts are displayed!
Not seeing permissions prompts? First, make sure you’ve added permissions to your manifest. Second, check your device settings to make sure you haven’t already granted location permissions.
Foreground tracking
Once the user has granted foreground permissions, you can track the user’s location in the foreground. To track the user’s location in the foreground, call:RadarTrackCallback with an implementation of onComplete() that receives the request status, the user’s location, the events generated, if any, and the user. The request status can be:
RadarStatus.SUCCESS: successRadarStatus.ERROR_PUBLISHABLE_KEY: SDK not initializedRadarStatus.ERROR_PERMISSIONS: location permissions not grantedRadarStatus.ERROR_LOCATION: location services error or timeout (10 seconds)RadarStatus.ERROR_NETWORK: network error or timeout (10 seconds)RadarStatus.ERROR_BAD_REQUEST: bad request (missing or invalid params)RadarStatus.ERROR_UNAUTHORIZED: unauthorized (invalid API key)RadarStatus.ERROR_PAYMENT_REQUIRED: payment required (organization disabled or usage exceeded)RadarStatus.ERROR_FORBIDDEN: forbidden (insufficient permissions)RadarStatus.ERROR_NOT_FOUND: not foundRadarStatus.ERROR_RATE_LIMIT: too many requests (rate limit exceeded)RadarStatus.ERROR_SERVER: internal server errorRadarStatus.ERROR_UNKNOWN: unknown error
Background tracking for geofencing
Once you have initialized the SDK and the user has authorized background permissions, you can start tracking the user’s location in the background. The SDK supports custom tracking options as well as three presets. For geofencing, we recommend usingRadarTrackingOptions.RESPONSIVE. This preset detects whether the device is stopped or moving. When moving, it tells the SDK to send location updates to the server every 2-3 minutes. When stopped, it tells the SDK to shut down to save battery. Once stopped, the device will need to move more than 100 meters to wake up and start moving again.
Assuming the user has authorized background permissions, background tracking will work even if the app has been backgrounded or killed, as Android location services will wake up the app to deliver events and the SDK uses JobScheduler to schedule network requests.
Though we recommend using presets for most use cases, you can modify the presets. See the tracking options reference.
Background tracking for trips
For trips, we recommend usingRadarTrackingOptions.continuous. This preset tells the SDK to send location updates to the server every 30 seconds, regardless of whether the device is moving.
To avoid Background Location Limits, the continuous preset starts a foreground service with a notification while tracking. When the foreground service with a notification is running, only foreground permissions are required for tracking. See the tracking options reference to adjust the foreground service notification copy and behavior.
Tracking for the duration of a trip is started or updated by including tracking options in the startTrip call:
Mock tracking for testing
Can’t go for a walk or a drive? You can simulate a sequence of location updates. For example, to simulate a sequence of 10 location updates every 3 seconds by car from an origin to a destination, call:Location metadata
The SDK can also pass along information like motion activity detection, speed and heading via the location metadata field. Turn onuseMotion in your sdkConfiguration to enable this feature. Include permissions for motion activity detection in your manifest
Listening for events with a receiver
To listen for events, location updates, and errors client-side, create a class that extendsRadarReceiver. Then, pass in an instance of your receiver when initializing the SDK in application onCreate().
Manual tracking
If you want to manage location services yourself, you can manually update the user’s location instead by calling:location is a Location instance with a valid latitude, longitude, and accuracy.
Identify user
The SDK automatically collectsinstallId (a GUID generated on fresh install) and deviceId (Android ID).
You can also assign a custom userId, also called External ID in the dashboard. To set a custom userId, call:
userId is a stable unique ID for the user.
To set a dictionary of custom metadata for the user, call:
metadata is a JSONObject with up to 16 keys and values of type string, boolean, or number.
Finally, to set an optional description for the user, displayed in the dashboard, call:
Debug logging
By default, only critical errors are logged to the console. To enable debug logging, call:Other APIs
The Android SDK also exposes APIs for beacons, geocoding, search, and distance.Beacons
To range and monitor beacons, you must request Bluetooth permissions. If targeting API level31 or above, both BLUETOOTH_CONNECT and BLUETOOTH_SCAN are required runtime permissions.
The following bluetooth permissions must be added to your manifest:
Geocoding
With the forward geocoding API, geocode an address, converting address to coordinates:Search
With the autocomplete API, autocomplete partial addresses and place names, sorted by relevance:Distance
With the distance API, calculate the travel distance and duration from an origin to a destination:Matrix
With the matrix API, calculate the travel distance and duration between multiple origins and destinations for up to 25 routes:Conversions
With the conversions API, log a conversion, such as a purchase or signup, to analyze alongside your app’s location activity:User tags
With the user tag API, you can configure campaigns to only target users with the corresponding tags:Notifications
WithsetNotificationOptions, set the icon and the background color of foreground service and event notifications:
Huawei
By default, the SDK depends on Google Play Services Location. However, you can use Huawei Mobile Services Location Kit instead. If you haven’t already configured your project for Huawei Mobile Services, follow the instructions here. Then, add the dependency to your app’sbuild.gradle file:
RadarLocationServicesProvider.HUAWEI when you initialize the SDK: