A powerful library for easy implementation of HMS Location Kit. ๐
- Request location with couple lines of code (no more boilerplate)
- Care more about battery consumption when requesting location
- Reach location in the background with ease
- Auto Location Permission Request
- Requesting Location Updates by caring battery consumption
- Requesting Location Updates with custom values
- Current Location (One time request)
- Last Known Location (One time request)
- Background Location Updates
- Auto Activity Recognition Permission Request
- Activity Type Recognition
You can reach Demo app from here
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
...
implementation 'com.github.talhaoz:advanced-location:1.1.1'
}
Add agconnect-services.json file under app directory that you get from AG Connect console
// Creates an AdvancedLocation object
val advancedLocation = AdvancedLocation()
/**
* @param activity is required to check for location permission
* @param locationType can be HIGH_ACCURACY, EFFICIENT_POWER, LOW_POWER or PASSIVE
* @param interval interval(refresh frequency) --> default value = 0L
* @param resultListener returns an object of position(latitude,longitude)
*/
advancedLocation.requestLocationUpdates(
this,
LocationType.EFFICIENT_POWER,
UpdateInterval.INTERVAL_15_SECONDS
) {
Log.d(TAG, "Lat: ${it.latitude}\n Long: ${it.longitude}")
}
/**
* @param activity is required to check for location permission
* @param interval interval(refresh frequency) --> default value = 0L
* @param smallestDisplacement(max difference between positions) --> default value = 0F
* @param resultListener returns an object of position(latitude,longitude)
*/
advancedLocation.requestCustomLocationUpdates(
this,
interval = 20L,
smallestDisplacement = 1F
) {
Log.d(TAG, "Lat: ${it.latitude}\n Long: ${it.longitude}")
}
advancedLocation.removeLocationUpdateRequest()
/**
* @param activity is required for permission
* @param resultListener returns the position(latitude,longitude)
*/
advancedLocation.getCurrentLocation(this) {
Log.d(TAG, "Lat: ${it.latitude}\n Long: ${it.longitude}")
}
/**
* @param activity is required in case initialization fails
* @param taskListener returns the position(latitude,longitude) on success
* or returns an exception on failure
*/
advancedLocation.getLastLocation(this) {
val position = it.value
Log.d(TAG, "Lat: ${position.latitude}\n Long: ${position.longitude}")
}
/**
* Requests Location Updates by running a foreground service and stores them in the Room DB (including old locations)
*
* @param activity is required for permission
* @param notificationTitle is title for Notification that displayed along with foreground service (if Empty app name will be displayed)
* @param notificationDescription is description for Notification that displayed along with foreground service(if Empty default desc will be displayed)
* @param updateInterval location update refresh frequency (Default value = 5 mins)
* @returns BackgroundLocationResult that accesses to RoomDB (only get)
*/
advancedLocation.startBackgroundLocationUpdates(
this,
"Advanced Location Demo",
"Reaching your location in background..",
UpdateInterval.INTERVAL_FIVE_MINUTES
).let {
val list = it.getAllLocationUpdates()
list?.forEach { location ->
Log.d(TAG, "### Lat: ${location.latitude}\n Long: ${location.longitude}")
}
}
advancedLocation.stopBackgroundLocationUpdates()
advancedLocation.clearLocationDB()
// Requests the current activity type and stores the result in the Room DB
advancedLocation.getActivityType(
this
).let { activityTypeResult ->
val activityType = activityTypeResult.getActivityType()
activityType.let {
Log.d(TAG, "Activity Type: ${it?.type}")
}
}
advancedLocation.clearActivityTypeDB()
MIT License
Copyright (c) 2022 Talha รZ