android-location-tracker


Source link: https://github.com/quentin7b/android-location-tracker

android-location-tracker

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

Installation

Add this to your build.gradle file

repositories {

  maven {

url "https://jitpack.io"
  
}
 
}
  dependencies {

compile 'com.github.quentin7b:android-location-tracker:3.2' 
}

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

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

Be aware of Android Marshmallow's new permission system

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

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

// You can pass an ui Context but it is not mandatory getApplicationContext() would also works // Be aware if you target android 23, you'll need to handle the runtime-permissions ! // see http://developer.android.com/reference/android/support/v4/content/ContextCompat.html if (
 ContextCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
  && ContextCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

// You need to ask the user to enable the permissions 
}
 else {

  LocationTracker tracker = new LocationTracker(ctx) {

@Override

public void onLocationFound(Location location) {

 // Do some stuff

}

  
}
;
  tracker.startListening();
 
}

And it's done, as soon as a location has been found, it will call the onLocationFound() method and you can do the job.

Provide custom use

You can call a LocationTracker with custom parameters. To do this, use the following constructor

LocationTracker(Context context, TrackerSettings settings)

As an example:

LocationTracker tracker = new LocationTracker(  context,  new TrackerSettings()

 .setUseGPS(true)

 .setUseNetwork(false)

 .setUsePassive(false)

) {

@Override  public void onLocationFound(Location location) {

// Do some stuff when a new GPS Location has been found  
}
 
}
; tracker.startListening()

This, will call a location tracker that is looking ONLY for GPS updates. Network and Passive updates will not be catched by the Tracker.

Providers AND metrics custom use

LocationTracker allows you to define some custom metrics like

  • The minimum time interval between location updates, in milliseconds
  • The minimum distance between location updates, in meters

To specify those parameters, LocationTracker you can set more settings. Here is an example of call:

TrackerSettings settings =

new TrackerSettings()

 .setUseGPS(true)

 .setUseNetwork(true)

 .setUsePassive(true)

 .setTimeBetweenUpdates(30 * 60 * 1000)

 .setMetersBetweenUpdates(100);

  LocationTracker tracker = new LocationTracker(context, settings) {

@Override  public void onLocationFound(Location location) {

// Do some stuff when a new location has been found.  
}
 
}
; tracker.startListening();

In this case, when a location is found, the tracker will not call onLocationFound() again during 30 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 time parameter's priority is higher than the distance parameter. So even if the user has moved from 2km, the tracker will call onLocationFound() only after 30 minutes.

Manage the tracker

By default, after a LocationTracker is created, it automatically starts listening to updates... and never stops. LocationTracker has two methods to start and stop listening for updates.

If you want to stop listening for updates, just call the stopListening() method. For example, if you need a one shot position, you can do that:

LocationTracker tracker = new LocationTracker(context) {

@Override  public void onLocationFound(Location location) {

// Stop listening for updates
stopListening()
// Do some stuff when a new GPS Location has been found  
}
 
}
; tracker.startListening();

You can also do it in the onPause() Activity method if you want.

@Override protected void onPause() {
  if(myTracker != null) {

myTracker.stopListening();
  
}
  super.onPause();
 
}

REMEMBER! A LocationTracker never stops untill you tell it to do so.

You may want to start listening for updates after all. To do that, LocationTracker has a public method named startListening(), call it when you want.

For example, in the onResume() Activity method:

@Override protected void onResume() {
  if(myTracker != null) {

myTracker.startListening();
  
}
  super.onResume();
 
}

Overriding

LocationTracker implements Android's LocationListener interface.

By default, it does nothing but logging.

Excepts the onLocationChanged() method, you can override all the LocationListener's metods, so here is the list:

  • onProviderDisabled(String provider)
  • onProviderEnabled(String provider)
  • onStatusChanged(String provider, int status, Bundle extras)

Contact & Questions

If you have any questions, fell free to send me a mail. You can also fork this project, or open an issue :)

Resources

Sample project showing usage of AutoValue library.

This project holds adapters for combining Realm Java with Android UI components and framework classes.

Another sliding menu base on DrawerLayout.

AutoTypeTextView is simple library which add encryption, decryption and texting animations.

Android library allowing to preserve instance of any object across orientation changes.

Android Library for auto-formatting money on EditText.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes