Last active
April 29, 2022 04:25
-
-
Save emil2k/5381596 to your computer and use it in GitHub Desktop.
Android utility class for getting device location using various methods. Depends on `Connectivity` class found here: https://gist.github.com/emil2k/5130324
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2017 Emil Davtyan | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining | |
* a copy of this software and associated documentation files (the | |
* "Software"), to deal in the Software without restriction, including | |
* without limitation the rights to use, copy, modify, merge, publish, | |
* distribute, sublicense, and/or sell copies of the Software, and to | |
* permit persons to whom the Software is furnished to do so, subject to | |
* the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be | |
* included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
* | |
*/ | |
package com.emil.android.util; | |
import android.content.Context; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.util.Log; | |
/** | |
* Get device location using various methods | |
* | |
* @author emil http://stackoverflow.com/users/220710/emil | |
*/ | |
public class Locator implements LocationListener { | |
static private final String LOG_TAG = "locator"; | |
static private final int TIME_INTERVAL = 100; // minimum time between updates in milliseconds | |
static private final int DISTANCE_INTERVAL = 1; // minimum distance between updates in meters | |
static public enum Method { | |
NETWORK, | |
GPS, | |
NETWORK_THEN_GPS | |
} | |
private Context context; | |
private LocationManager locationManager; | |
private Locator.Method method; | |
private Locator.Listener callback; | |
public Locator(Context context) { | |
super(); | |
this.context = context; | |
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); | |
} | |
public void getLocation(Locator.Method method, Locator.Listener callback) { | |
this.method = method; | |
this.callback = callback; | |
switch (this.method) { | |
case NETWORK: | |
case NETWORK_THEN_GPS: | |
Location networkLocation = this.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
if (networkLocation != null) { | |
Log.d(LOG_TAG, "Last known location found for network provider : " + networkLocation.toString()); | |
this.callback.onLocationFound(networkLocation); | |
} else { | |
Log.d(LOG_TAG, "Request updates from network provider."); | |
this.requestUpdates(LocationManager.NETWORK_PROVIDER); | |
} | |
break; | |
case GPS: | |
Location gpsLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
if (gpsLocation != null) { | |
Log.d(LOG_TAG, "Last known location found for GPS provider : " + gpsLocation.toString()); | |
this.callback.onLocationFound(gpsLocation); | |
} else { | |
Log.d(LOG_TAG, "Request updates from GPS provider."); | |
this.requestUpdates(LocationManager.GPS_PROVIDER); | |
} | |
break; | |
} | |
} | |
private void requestUpdates(String provider) { | |
if (this.locationManager.isProviderEnabled(provider)) { | |
if (provider.contentEquals(LocationManager.NETWORK_PROVIDER) | |
&& Connectivity.isConnected(this.context)) { | |
Log.d(LOG_TAG, "Network connected, start listening : " + provider); | |
this.locationManager.requestLocationUpdates(provider, TIME_INTERVAL, DISTANCE_INTERVAL, this); | |
} else if (provider.contentEquals(LocationManager.GPS_PROVIDER) | |
&& Connectivity.isConnectedMobile(this.context)) { | |
Log.d(LOG_TAG, "Mobile network connected, start listening : " + provider); | |
this.locationManager.requestLocationUpdates(provider, TIME_INTERVAL, DISTANCE_INTERVAL, this); | |
} else { | |
Log.d(LOG_TAG, "Proper network not connected for provider : " + provider); | |
this.onProviderDisabled(provider); | |
} | |
} else { | |
this.onProviderDisabled(provider); | |
} | |
} | |
public void cancel() { | |
Log.d(LOG_TAG, "Locating canceled."); | |
this.locationManager.removeUpdates(this); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
Log.d(LOG_TAG, "Location found : " + location.getLatitude() + ", " + location.getLongitude() + (location.hasAccuracy() ? " : +- " + location.getAccuracy() + " meters" : "")); | |
this.locationManager.removeUpdates(this); | |
this.callback.onLocationFound(location); | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
Log.d(LOG_TAG, "Provider disabled : " + provider); | |
if (this.method == Locator.Method.NETWORK_THEN_GPS | |
&& provider.contentEquals(LocationManager.NETWORK_PROVIDER)) { | |
// Network provider disabled, try GPS | |
Log.d(LOG_TAG, "Requesst updates from GPS provider, network provider disabled."); | |
this.requestUpdates(LocationManager.GPS_PROVIDER); | |
} else { | |
this.locationManager.removeUpdates(this); | |
this.callback.onLocationNotFound(); | |
} | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
Log.d(LOG_TAG, "Provider enabled : " + provider); | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
Log.d(LOG_TAG, "Provided status changed : " + provider + " : status : " + status); | |
} | |
public interface Listener { | |
void onLocationFound(Location location); | |
void onLocationNotFound(); | |
} | |
} |
What is Connectivity ?
for those who struggle to find the Connectivity - http://stackoverflow.com/a/8548926/710817
@mpraki @mychaelgo Sorry, these were all part of a utility package I made for an old app and I didn't check them closely enough before posting. I added a note in the description of this gist with a link to the Connectivity
class gist.
How to use this in an activity?
Add this class to your project, it requires context. then by using that reference object call get location method written in this class.
How to use this in an activity?
How to print the location
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great.
But for API 23 or above, you should check permission before using the LocationManager.
I need to add the following checking for each method before calling LocationManager in order to compile.