Last active
April 22, 2016 16:08
-
-
Save lalbuquerque/93d72d1ca803648be8ca to your computer and use it in GitHub Desktop.
Try to get location by GPS first and then, if it is disabled, by Network
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
private Location getLocation(){ | |
long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10L; | |
long MIN_TIME_BW_UPDATES = 1; | |
Location location; | |
try { | |
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); | |
boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); | |
boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); | |
if (isGPSEnabled) { | |
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); | |
Log.d("GPS Enabled", "GPS Enabled"); | |
if (mLocationManager != null) { | |
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
if (location != null) { | |
return location; | |
} | |
} | |
} | |
if (isNetworkEnabled) { | |
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); | |
Log.d("Network", "Network"); | |
if (mLocationManager != null) { | |
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
if (location != null) { | |
return location; | |
} | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment