Source: https://developers.woosmap.com/products/geofencing-sdk/android-sdk/guides/setup/

> For clean Markdown of any page, append `.md` to the page URL.

> For a complete documentation index, see https://developers.woosmap.com/llms.txt

# Android SDK



The Woosmap Geofencing SDK allows you to monitor Geofences, track your user's location and connect with the Woosmap Search and Distance APIs.

Learn how to integrate the Android SDK below. You can also explore [a part of the source code on GitHub](https://github.com/Woosmap/geofencing-core-android-sdk) to get deeper into how user location is collected.

Find the release notes for the Geofencing Android SDK [here](https://woosmap.github.io/geofencing-enterprise-android-sdk-changelog/).

## Install SDK

You can install the SDK using **Gradle**. Note that the SDK is small and adds 400 KB to your compiled app. Geofencing SDK along with it's dependencies adds an overhead of around 4 MB to the compiled APK.

#### Gradle via Maven central

Add the **Maven central** repository to your `build.gradle` file at the end of _repositories_, and add the dependency as follows:

```gradle
dependencies {
    implementation 'com.webgeoservices.woosmapgeofencing:woosmap-mobile-sdk:+'
}
```

## Dependencies

The SDK requires Android 9 or newer, Google Play Services Location, and the open source Java
library <https://www.ojalgo.org/> used for matrix and equations calculations.

The SDK currently supports API level 28 and higher.

## Initialize SDK

To retrieve your user's location data when your app starts, initialize the SDK in `MainActivity` through
the `onCreate()` method. You'll need to create a Private API Key with the proper restriction in the console. See the [API Key documentation](/api-reference/authentication/#registering-a-woosmap-private-api-key) for details.

The SDK automatically sends the `X-Api-Key`, `X-Android-Identifier`, and `X-Android-Fingerprint` headers with every API request. If you're calling Woosmap REST APIs directly without the SDK, you'll need to set these headers yourself. See [Authenticating from Mobile Apps](/api-reference/authentication/#authenticating-from-mobile-apps-or-server-side-via-headers) for details.

```java
import com.webgeoservices.woosmapgeofencing.Woosmap;
import com.webgeoservices.woosmapgeofencing.WoosmapSettings;
import com.webgeoservices.woosmapgeofencingcore.database.*;

public class MainActivity extends AppCompatActivity {
    private Woosmap woosmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate();

        this.woosmap = Woosmap.getInstance().initializeWoosmap(this);
        this.woosmap.setLocationReadyListener(new WoosLocationReadyListener());
    }
}
```

Then, create a Listener connect to the interface `Woosmap.LocationReadyListener` and set a callback to retrieve user
current location.

```java
public class WoosLocationReadyListener implements Woosmap.LocationReadyListener {
    public void LocationReadyCallback(Location location) {
      onLocationCallback(location);
    }
}
private void onLocationCallback(Location currentLocation) {
    //Work with the user Location here
}
```

To get location consistently, call `woosmap.onResume()` and `woosmap.onPause()` functions on corresponding method of
the App [Activity Lifecycle](https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle).

```java
@Override
public void onResume() {
    super.onResume();
    if (checkPermissions()) {    //Permission to get Location OK
        this.woosmap.onResume();
    } else {                     //Not alowed so Ask to Share Location
        requestPermissions();
    }
}

@Override
public void onPause(){
    super.onPause();
    if (checkPermissions()) {
        this.woosmap.onPause(); //Get Location In Background
    }
}
```
