项目作者: quentin7b

项目描述 :
Android helper that tracks user location
高级语言: Kotlin
项目地址: git://github.com/quentin7b/android-location-tracker.git
创建时间: 2013-11-24T17:08:46Z
项目社区:https://github.com/quentin7b/android-location-tracker

开源协议:MIT License

下载


android-location-tracker

Android Simple Location Tracker is an Android library that helps you get user location with a object named LocationTracker

Android Arsenal

Installation

Add this to your build.gradle file

  1. repositories {
  2. maven {
  3. url "https://jitpack.io"
  4. }
  5. }
  6. dependencies {
  7. compile 'com.github.quentin7b:android-location-tracker:4.0'
  8. }

Don’t forget to add the following permissions to your AndroidManifest.xml

  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" ></uses-permission>
  2. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" ></uses-permission>

Be aware of Android Marshmallow‘s new permission system

  1. <permission-group
  2. android:name="android.permission-group.LOCATION"
  3. android:label="A label for your permission"
  4. android:description="A description for the permission" ></permission-group>

Use

As its name says, it’s a simple library.
To create a tracker you just need to add the below code in your Android Activity / Service / WorkManager

Be aware, you’ll have to manage runtime permissions on Manifest.permission.ACCESS_FINE_LOCATION & Manifest.permission.ACCESS_COARSE_LOCATION

Create the tracker

Constructor is defined as this

  1. val locationTracker = LocationTracker(
  2. val minTimeBetweenUpdates: Long = 5 * 60 * 1000.toLong(),
  3. val minDistanceBetweenUpdates: Float = 100f,
  4. val shouldUseGPS: Boolean = true,
  5. val shouldUseNetwork: Boolean = true,
  6. val shouldUsePassive: Boolean = true
  7. )

Add a listener

  1. locationTracker.addListener(object: Listener {
  2. fun onLocationFound(location: Location) {
  3. }
  4. fun onProviderError(providerError: ProviderError) {
  5. }
  6. });

Start and stop listening

  1. locationTracker.startListening(context)
  2. //and
  3. locationTracker.stopListening()

Provide custom use

You can create a LocationTracker with custom parameters.

  • minTimeBetweenUpdates minimum time between two locations to respect before notifying the listeners in milliseconds). Default is 5 minutes
  • minDistanceBetweenUpdates minimum distance between two locations to respect before notifying the listeners in meters). Default is 100 meters
  • shouldUseGPS specifies if the tracker should use the GPS locations or not. Default is true
  • shouldUseNetwork specifies if the tracker should use the GPS locations or not. Default is true
  • shouldUsePassive specifies if the tracker should use the passive locations or not. Default is true

With the default parameters, when a location is found, the tracker will not call onLocationFound() again during 5 minutes.
Moreover, if the distance between the new location and the older one is less than 100m, onLocationFound() will not be called.

Be aware that the priority of the time parameter is higher than the priority of the distance parameter.
So even if the user has moved from 200km, the tracker will call onLocationFound() only after 30 minutes.

Cleaning

Be aware! A LocationTracker never stops running until you tell it to do so.

If the tracker is running in foreground and not in a service, it might be a good idea to link to the lifecycle

The stopListening method has an optional parameter cleanListeners that is false by default.
(calling stopListening(false) is the same as calling stopListening()).

When calling stopListening we do not remove the listeners you’ve set, so they will be notified once you start listening again.
But calling stopListening(true) will clear the list of registered listeners (same as calling removeListener for every registered listener).

  1. tracker.addListener(listener)
  2. tracker.startListening(context)
  3. // listener will be notified
  4. ...
  5. tracker.stopListening()
  6. // listener won't receive updated
  7. tracker.startListening(context)
  8. // listener will be notified
  9. ...
  10. tracker.stopListening(cleanListeners = true)
  11. // listener won't receive updated for ever
  12. tracker.startListening(context)
  13. // listener won't be notified