use of android.location.LocationManager in project Parse-SDK-Android by ParsePlatform.
the class LocationNotifier method getCurrentLocationAsync.
/**
* Asynchronously gets the current location of the device.
*
* <p>This will request location updates from the best provider that match the given criteria
* and return the first location received.
*
* <p>You can customize the criteria to meet your specific needs. * For higher accuracy, you can
* set {@link Criteria#setAccuracy(int)}, however result in longer times for a fix. * For better
* battery efficiency and faster location fixes, you can set {@link
* Criteria#setPowerRequirement(int)}, however, this will result in lower accuracy.
*
* @param context The context used to request location updates.
* @param timeout The number of milliseconds to allow before timing out.
* @param criteria The application criteria for selecting a location provider.
* @see android.location.LocationManager#getBestProvider(android.location.Criteria, boolean)
* @see android.location.LocationManager#requestLocationUpdates(String, long, float,
* android.location.LocationListener)
*/
/* package */
static Task<Location> getCurrentLocationAsync(Context context, long timeout, Criteria criteria) {
final TaskCompletionSource<Location> tcs = new TaskCompletionSource<>();
final Capture<ScheduledFuture<?>> timeoutFuture = new Capture<>();
final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
final LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location == null) {
return;
}
timeoutFuture.get().cancel(true);
tcs.trySetResult(location);
manager.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
timeoutFuture.set(ParseExecutors.scheduled().schedule(() -> {
tcs.trySetError(new ParseException(ParseException.TIMEOUT, "Location fetch timed out."));
manager.removeUpdates(listener);
}, timeout, TimeUnit.MILLISECONDS));
String provider = manager.getBestProvider(criteria, true);
if (provider != null) {
manager.requestLocationUpdates(provider, /* minTime */
0, /* minDistance */
0.0f, listener);
}
if (fakeLocation != null) {
listener.onLocationChanged(fakeLocation);
}
return tcs.getTask();
}
use of android.location.LocationManager in project glasquare by davidvavra.
the class LocationUtils method getLastLocation.
public static Location getLastLocation() {
LocationManager manager = (LocationManager) App.get().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
List<String> providers = manager.getProviders(criteria, true);
List<Location> locations = new ArrayList<Location>();
for (String provider : providers) {
Location location = manager.getLastKnownLocation(provider);
if (location != null && location.getAccuracy() != 0.0f) {
locations.add(location);
}
}
Collections.sort(locations, new Comparator<Location>() {
@Override
public int compare(Location location, Location location2) {
return (int) (location.getAccuracy() - location2.getAccuracy());
}
});
if (locations.size() > 0) {
return locations.get(0);
}
return null;
}
use of android.location.LocationManager in project simplefacebook by androidquery.
the class PlaceActivity method refreshButton.
private void refreshButton() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (enabled) {
aq.id(R.id.button_gps).gone();
} else {
aq.id(R.id.button_gps).visible();
}
refreshHint();
}
use of android.location.LocationManager in project weiciyuan by qii.
the class WriteWeiboActivity method addLocation.
private void addLocation() {
LocationManager locationManager = (LocationManager) WriteWeiboActivity.this.getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Toast.makeText(WriteWeiboActivity.this, getString(R.string.please_open_gps), Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(WriteWeiboActivity.this, getString(R.string.gps_is_searching), Toast.LENGTH_SHORT).show();
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
}
}
use of android.location.LocationManager in project weiciyuan by qii.
the class WriteWeiboActivity method updateWithNewLocation.
private void updateWithNewLocation(Location result) {
haveGPS.setVisibility(View.VISIBLE);
geoBean = new GeoBean();
geoBean.setLatitude(result.getLatitude());
geoBean.setLongitude(result.getLongitude());
if (Utility.isTaskStopped(locationTask)) {
locationTask = new GetGoogleLocationInfo(geoBean);
locationTask.executeOnExecutor(MyAsyncTask.THREAD_POOL_EXECUTOR);
}
((LocationManager) WriteWeiboActivity.this.getSystemService(Context.LOCATION_SERVICE)).removeUpdates(locationListener);
}
Aggregations