Search in sources :

Example 66 with LocationManager

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();
}
Also used : LocationManager(android.location.LocationManager) TaskCompletionSource(com.parse.boltsinternal.TaskCompletionSource) Bundle(android.os.Bundle) LocationListener(android.location.LocationListener) ScheduledFuture(java.util.concurrent.ScheduledFuture) Capture(com.parse.boltsinternal.Capture) Location(android.location.Location)

Example 67 with LocationManager

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;
}
Also used : LocationManager(android.location.LocationManager) ArrayList(java.util.ArrayList) Criteria(android.location.Criteria) Location(android.location.Location)

Example 68 with LocationManager

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();
}
Also used : LocationManager(android.location.LocationManager)

Example 69 with LocationManager

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);
    }
}
Also used : LocationManager(android.location.LocationManager)

Example 70 with LocationManager

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);
}
Also used : LocationManager(android.location.LocationManager) GeoBean(org.qii.weiciyuan.bean.GeoBean)

Aggregations

LocationManager (android.location.LocationManager)151 Location (android.location.Location)51 Bundle (android.os.Bundle)33 LocationListener (android.location.LocationListener)29 Criteria (android.location.Criteria)21 Intent (android.content.Intent)20 BluetoothAdapter (android.bluetooth.BluetoothAdapter)15 View (android.view.View)10 SuppressLint (android.annotation.SuppressLint)9 IOException (java.io.IOException)9 ConnectivityManager (android.net.ConnectivityManager)7 WifiManager (android.net.wifi.WifiManager)7 TextView (android.widget.TextView)7 DialogInterface (android.content.DialogInterface)6 IntentFilter (android.content.IntentFilter)6 SharedPreferences (android.content.SharedPreferences)6 PendingIntent (android.app.PendingIntent)5 NetworkInfo (android.net.NetworkInfo)5 TelephonyManager (android.telephony.TelephonyManager)5 TrackerDataHelper (com.android.locationtracker.data.TrackerDataHelper)5