Skip to content

Android

Prateek Srivastava edited this page Jan 20, 2018 · 75 revisions

digitreck Android SDK intends to simplify the usage of digitreck features like location repository, real-time tracking and places with a nice fluid API.

Supported Android SDK versions: 16+

Supported Google Play Services compatible version: 11.0.2+

1. Add gradle dependency

The fastest and easiest way to use digitreck in your project is with Gradle.

  • Add digitreck maven repository to repositories in project's build.gradle
apply plugin: 'com.android.application'

allprojects {
  repositories {
    jcenter()
    maven {
      url 'https://maven.digitreck.com/public'
    }
  }
}
  • Add digitreck-base dependency
implementation 'com.google.android.gms:play-services-location:11.0.2'
implementation 'com.digitreck.android:base:1.3'

2. Initialize the SDK

  • Initiate the SDK
OnSetupListener setupListener = new OnSetupListener() {
  @Override
  public void onReady(String entity) {
    // triggered on successful setup
  }

  @Override
  public void onFailed(String error) {
    // triggered on setup failed
  }
};
Configuration config = new Configuration.Builder(this)
      .group(YOUR_GROUP_ID)
      .entity(UNIQUE_ENTITY_IDENTIFIER)
      .environment(Environment.PROD) /* PROD or SANDBOX, default: SANDBOX */
      .mode(LocationMode.FUSED) /* FUSED or PASSIVE, default: FUSED*/
      .build();
Digitreck.init(config, setupListener);
  • Terminate the SDK
Digitreck.terminate();

3. Starting location service

To start syncing device location to repository, extend DigitreckActivity.java in your activity and call the following method.

  • Sync device location to repository
Digitreck.startSync(this, new OnLocationServiceStateListener() {
  @Override
  public void onStarted() {
    // triggered when location service has started successfully
  }

  @Override
  public void onCancelled(int reason) {
    // triggered when location service is cancelled, with reason for cancel
    // reason = 1 : location permission denied
    // reason = 2 : location service denied

  }
});
  • Stoping Location updates
Digitreck.stopSync();

4. Next Steps

Congratulations! You have successfully performed the basic digitreck integration and the started the location service. Now you are ready to perform tasks like listening location updates, realtime tracking of other devices and listening to places.

Requesting Location Updates

private OnLocationChangeListener locationListener = new OnLocationChangeListener() {
  @Override
  public void onLocationChanged(@Nonnull Location location) {
    //triggered when location change is detected
  }
};
  • Requesting Location Updates
Digitreck.LocationApi.subscribe(locationListener);
  • Cancel Location Updates
Digitreck.LocationApi.unsubscribe(locationListener);

Real-time tracking

OnRealtimeEventListener realtimeListener = new OnRealtimeEventListener(REALTIME_SESSION_ID) {
  @Override
  public void onUpdate(@Nonnull HashMap<String, Location> data) {
    //triggered when new location data is received
  }
};
  • Requesting for tracking
Digitreck.RealtimeSessionsApi.subscribe(realtimeListener);
  • Cancel tracking
Digitreck.RealtimeSessionsApi.unsubscribe(realtimeListener);

Places

OnPlaceUpdateListener placesListener = new OnPlaceUpdateListener() {
  @Override
  public void onEnter(@Nonnull Place place) {
    //when device reaches near some place.
  }

  @Override
  public void onExit(@Nonnull Place place) {
    //when device moves away from already reached place.
  }
};
  • Requesting for Place Events
Digitreck.PlacesApi.subscribe(placesListener);
  • Cancel Place Events
Digitreck.PlacesApi.unsubscribe(placesListener);
Clone this wiki locally