Mobile

Location Tracking

Implement the tracking of user's location in your Android App

The Location Tracking starts after calling woosmap.updateUserTracking(true);.

Prerequisites

To enable the location tracking, add the Google Location and Activity Recognition from Google Play services client library by specifying the following code in your build.gradle file.

dependencies {
    implementation 'com.google.android.gms:play-services-location:15.0.1'
}

You’ll also need to declare various permissions in your AndroidManifest.xml file.

<!-- Allows an app to open network sockets. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allows an app to access precise location. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Allows an app to access approximate location. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Allows applications to access information about networks. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Permissions

Once you’ve declared the needed above APIs in your AndroidManifest.xml file, ensure that you have the required permission before enabling Woosmap Tracking.

Verify permissions

This can be done using the checkSelfPermission method of ActivityCompat or ContextCompat. If the call returns false this means the permissions aren’t granted and you should use requestPermissions to request them. The response to this is returned in a callback which you will see in the next step.

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
  // Check Permissions Now
  private static final int REQUEST_LOCATION = 2;
  
  ActivityCompat.requestPermissions(this,
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
        REQUEST_LOCATION);
} else {
  // permission has been granted, start tracking with Woosmap
  this.woosmap.updateUserTracking(true)  
}

Implement the request permission callback

If the permission your app needs hasn’t been granted by the user, the requestPermissions method should be called to ask the user to grant them. The response from the user is captured in the onRequestPermissionsResult callback. Your app should implement this and always check the return values because the request could be denied or canceled.

public void onRequestPermissionsResult(int requestCode,
                                       String[] permissions,
                                       int[] grantResults) {
    if (requestCode == REQUEST_LOCATION) {
        if(grantResults.length == 1
           && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // We can now safely call Woosmap to use the API we requested access to
            this.woosmap.updateUserTracking(true);
        } else {
            // Permission was denied or request was cancelled
            this.woosmap.updateUserTracking(false);
        }
    }
}

Show the permission rationale

Users are more likely to grant permissions when they understand the why the permission is needed and the immediate benefit for them.

In this case, before the calling requestPermissions, you should call shouldShowRequestPermissionRationale. If it returns true, you should create some UI to display additional context for the permission.

Activity Lifecycle

In order to track fine-grained user location and enable Woosmap Mobile to optimize for resources - device battery and data usage, implement the steps below in the Lifecycle callbacks onResume() and onPause().

private boolean checkPermissions() {
        int permissionState = ActivityCompat.checkSelfPermission(this,
                android.Manifest.permission.ACCESS_FINE_LOCATION);
        return permissionState == PackageManager.PERMISSION_GRANTED;
}

@Override
public void onResume() {
    super.onResume();
    if (checkPermissions()) {
        this.woosmap.onResume();
    } else {
        requestPermissions();
    }
}

@Override
public void onPause(){
    super.onPause();
    if (checkPermissions()) {
        this.woosmap.onPause();
    }
}

Eventually, it is recommended to take a look at our Woosmap Android Sample on GitHub.

Enable location after phone’s reboot

Create the BroadcastReceiver

To collect location after phone’s reboot without relaunch the application, create a BroadcastReceiver to launch the WoosmapRebootJobService on BOOT_COMPLETED event.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.webgeoservices.woosmap_mobile_sdk.WoosmapRebootJobService;

public class woosmapRunOnStartup extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            WoosmapRebootJobService.enqueueWork(context, new Intent());
        }
    }

}

Add the BroadcastReceiver to the Manifest

To allow your application to receive the Intent.ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting, add the following permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Finally, declare your receiver in AndroidManifest.xml in the <application> bloc.

<receiver android:name=".woosmapRunOnStartup">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

Next Step

Congratulations! You have now successfully integrated Woosmap Mobile SDK with your Android app and are now sending user data to Woosmap. We suggest you proceed to read the next section to learn how to Integrate push messaging.