The Roam Android SDK makes it quick and easy to build a location tracker for your Android app. We provide powerful and customizable tracking modes and features.
Requirements
To use the Roam SDK,
Get yourself a free Roam Account. No credit card is required.
Create a project and add an Android app to the project.
You need the PUBLISHABLE_KEY (available in your project settings) to initialize the SDK.
Now, you’re ready to integrate the SDK into your Android application.
Roam Android SDK requires Android Studio 2.0 or later and is compatible with apps targeting Android SDK Version 16 or above.
Run the example application
The Roam Example repository on GitHub includes sample applications that demonstrate the use of the v3 Roam SDK for Android.
To run an example app, clone this repository, navigate to the example app in paths Example/ , add your publishable YOUR-PUBLISHABLE-KEY key in MainApplication.java, and run the app.
Make sure the package name is the same as the one registered on our Roam dashboard.
Open Android Studio and add the SDK Roam.aar as a module using File > New > New Module > Import .JAR/.AAR Package.
Once Gradle is finished, click File > Project Structure again.
Click on the Dependencies tab > click App > click the “+” icon in the top left of the Declared Dependencies section > select Module Dependency > click on Roam-release > press Ok and wait for Gradle to sync again.
Make sure to include the dependencies separately and sync your project.
Roam SDK offers a flexible method of initialization, allowing developers to seamlessly integrate our services into their applications. With our latest update, we've introduced a new initialization approach using Manifest file. This method provides an effortless way to set up your Roam SDK without the need for additional code.
Using Manifest file Initialization:
Optional Parameter: Publish Key
The publishKey parameter in the initialization method is now optional.
If you've added your Roam publish key to your project's Manifest file, there's no need to pass the publishKey parameter during initialization.
Configure Your Manifest File:
In your Manifest file, declare your Roam publish key as follows:
Initialize Roam SDK in your application class as follows:
If the publish key is added to the Manifest file, the initialize method can be called without passing the publishable key.
Roam.initialize(this)
Roam.initialize(this);
By following these steps, you can seamlessly initialize Roam SDK using manifest file, simplifying the integration process and saving valuable development time.
Using Application class Initialization:
Before initializing the SDK, the below must be imported.
import com.roam.sdk.Roam;
After importing, add the code below under the Application class onCreate() method. The SDK must be initialized before calling any of the other SDK methods.
Roam.initialize(this, "YOUR-SDK-KEY-GOES-HERE")
Roam.initialize(this,"YOUR-SDK-KEY-GOES-HERE");
Request Permission
To request the location for devices running both below/above Android 10, refer to the following piece of code.
Specify the tracking modes while you use the Roam.startTrackingmethod.
Tracking Modes
Adaptive Tracking
Roam has three default tracking modes along with a custom version. They differ based on the frequency of location updates and battery consumption. The higher the frequency, the higher the battery consumption. You must use the foreground service for continuous tracking.
// active trackingRoam.startTracking(RoamTrackingMode.ACTIVE, object:TrackingCallback(){ override fun onSuccess(p0: String?) {//do something } override fun onError(p0: RoamError?) {//do something } })// balanced trackingRoam.startTracking(RoamTrackingMode.BALANCED, object:TrackingCallback(){ override fun onSuccess(p0: String?) {//do something } override fun onError(p0: RoamError?) {//do something } })// passive trackingRoam.startTracking(RoamTrackingMode.PASSIVE, object:TrackingCallback(){ override fun onSuccess(p0: String?) {//do something } override fun onError(p0: RoamError?) {//do something } })
Distance Interval Tracking
The SDK also provides a custom tracking mode that allows you to customize and build your own tracking modes.
With distance interval tracking you create a tracking mode with a distance interval in meters of your choice.
Type
Unit
Unit Range
Distance Interval
Meters
1m ~ 2500m
Distance between location updates example code:
// Define a custom tracking method with desired distance interval, stop duration and accuracyval trackingMode = RoamTrackingMode.Builder(<DISTANCE-FILTER-IN-METERS>, <STOP-INTERVAL-IN-SECONDS>) .setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH) .build()// Start the tracking with the above created custom tracking methodRoam.startTracking(trackingMode, object:TrackingCallback(){overridefunonSuccess(p0: String?) {//do something }overridefunonError(p0: RoamError?) {//do something } })
// Define a custom tracking method with desired distance interval, stop duration and accuracyRoamTrackingMode trackingMode =new RoamTrackingMode.Builder(<DISTANCE-FILTER-IN-METERS>,<STOP-INTERVAL-IN-SECONDS>).setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH).build();// Start the tracking with the above created custom tracking methodRoam.startTracking(trackingMode,newTrackingCallback() { @OverridepublicvoidonSuccess(String s) {//do something } @OverridepublicvoidonError(RoamError roamError) {//do something } });
Time Interval Tracking
With Time Interval Tracking you can create a tracking mode with the time interval (in seconds) of your choice.
Type
Unit
Unit Range
Time Interval
Seconds
10s ~ 10800s
Time between location updates example code:
// Define a custom tracking method with desired time interval and accuracyval trackingMode = RoamTrackingMode.Builder(<TIME-INTERVAL-IN-SECONDS>) .setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH) .build()// Start the tracking with the above created custom tracking methodRoam.startTracking(trackingMode, object:TrackingCallback(){overridefunonSuccess(p0: String?) {//do something }overridefunonError(p0: RoamError?) {//do something } })
// Define a custom tracking method with desired time interval and accuracyRoamTrackingMode trackingMode =new RoamTrackingMode.Builder(<TIME-INTERVAL-IN-SECONDS>).setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH).build();// Start the tracking with the above created custom tracking methodRoam.startTracking(trackingMode,newTrackingCallback() { @OverridepublicvoidonSuccess(String s) {//do something } @OverridepublicvoidonError(RoamError roamError) {//do something } });
You may see a delay if the user's device is in low power mode or has connectivity issues.
Listeners are needed to consume the location or event data from the SDK. To enable listeners, ensure the following:
To listen to location updates, create a class that extends RoamReceiver.
Register the receiver by adding a receiver element to the application element in your manifest.
Note: For self-tracking, you can only listen to only location, error, and offline trips status data since the locations are not being sent to our servers for processing events.
classLocationReceiver: RoamReceiver() {overridefunonLocationUpdated(context: Context?, roamLocations: MutableList<RoamLocation>?) {super.onLocationUpdated(context, roamLocations)// receive own location updates here// do something with location data using locationfor (roamLocation in roamLocations!!){ roamLocation.activity roamLocation.recordedAt roamLocation.timezoneOffset roamLocation.metadata roamLocation.batteryStatus roamLocation.networkStatus roamLocation.location.latitude roamLocation.location.longitude roamLocation.location.bearing roamLocation.location.altitude roamLocation.location.accuracy roamLocation.location.speed roamLocation.location.provider roamLocation.location.time roamLocation.location.verticalAccuracyMeters } }overridefunonError(context: Context?, roamError: RoamError?) {super.onError(context, roamError)// receive error message here roamError?.code roamError?.message }}
publicclassLocationReceiverextendsRoamReceiver { @OverridepublicvoidonLocationUpdated(Context context,List<RoamLocation> roamLocations) { super.onLocationUpdated(context, roamLocations);// receive own location updates here// do something with location data using locationfor(RoamLocation roamLocation: roamLocations){roamLocation.getActivity();roamLocation.getRecordedAt();roamLocation.getTimezoneOffset();roamLocation.getMetadata();roamLocation.getBatteryStatus();roamLocation.getNetworkStatus();roamLocation.getLocation().getLatitude();roamLocation.getLocation().getLongitude();roamLocation.getLocation().getBearing();roamLocation.getLocation().getAltitude();roamLocation.getLocation().getAccuracy();roamLocation.getLocation().getSpeed();roamLocation.getLocation().getProvider();roamLocation.getLocation().getTime();roamLocation.getLocation().getVerticalAccuracyMeters(); } } @OverridepublicvoidonError(Context context,RoamError roamError) { super.onError(context, roamError);// receive error message hereroamError.getCode(); roamError.getMessage(); }}
Batch Configuration
Batch configuration lets you control the number of location data updates being received in the location listener with the desired frequency and window.
Set Batch Configuration
As the name suggests, this method sets the configuration parameters.
The NetworkState.BOTH indicates the state in which the updates are to be received. It can either be set to online, offline, or both.
The batchCount indicates the size of the location batch.
The batchWindow indicates the time interval for every consecutive update (frequency of updates).
By default, the batch configuration values for both batch count and window are set to 1 and 0 respectively.
The batch count value is the number of location updates sent in a batch. In the above case, the value is set to 5. Ideally, the SDK sends 5 updates per batch and the batch window (in the above case, 60) specifies the number of seconds the SDK waits to receive five location updates.
If the SDK receives 5 location updates in less than 60 seconds, the updates are pushed to the listener. If not, it waits for 60 seconds and pushes the location updates regardless of the batch count value.
Success Callback - We receive a list of BatchReceiveConfig objects.