List of usage examples for android.location LocationManager requestSingleUpdate
@RequiresPermission(anyOf = { ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION }) public void requestSingleUpdate(Criteria criteria, LocationListener listener, Looper looper)
From source file:ch.hesso.master.sweetcity.activity.report.ReportActivity.java
void updateCurrentLocation() { if (this.locationListener == null) this.locationListener = new LocationListener() { @Override/*from ww w. j a va 2 s .c o m*/ public void onLocationChanged(Location location) { ReportActivity.this.currentLocation = location; } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }; Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(false); criteria.setHorizontalAccuracy(Criteria.ACCURACY_MEDIUM); criteria.setPowerRequirement(Criteria.NO_REQUIREMENT); criteria.setSpeedAccuracy(Criteria.NO_REQUIREMENT); criteria.setSpeedRequired(false); LocationManager locationManager = (LocationManager) ReportActivity.this.getSystemService(LOCATION_SERVICE); locationManager.requestSingleUpdate(criteria, this.locationListener, null); }
From source file:org.traccar.client.ShortcutActivity.java
@SuppressWarnings("MissingPermission") private void sendAlarm() { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String provider = PositionProvider.getProvider(preferences.getString(MainFragment.KEY_ACCURACY, "medium")); try {/*from w ww.j a v a 2 s. c o m*/ Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); if (location != null) { sendAlarmLocation(location); } else { locationManager.requestSingleUpdate(provider, new LocationListener() { @Override public void onLocationChanged(Location location) { sendAlarmLocation(location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }, Looper.myLooper()); } } catch (RuntimeException e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } }
From source file:com.google.android.dialer.provider.DialerProvider.java
private Location getLastLocation() { LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); locationManager.requestSingleUpdate(new Criteria(), new LocationListener() { @Override//from w ww . j a v a 2 s . com public void onLocationChanged(Location location) { if (Log.isLoggable("DialerProvider", Log.VERBOSE)) { Log.v("DialerProvider", "onLocationChanged: " + location); } } @Override public void onProviderDisabled(String provider) { if (Log.isLoggable("DialerProvider", Log.VERBOSE)) { Log.v("DialerProvider", "onProviderDisabled: " + provider); } } @Override public void onProviderEnabled(String provider) { if (Log.isLoggable("DialerProvider", Log.VERBOSE)) { Log.v("DialerProvider", "onProviderEnabled: " + provider); } } @Override public void onStatusChanged(String provider, int status, Bundle extras) { if (Log.isLoggable("DialerProvider", Log.VERBOSE)) { Log.v("DialerProvider", "onStatusChanged: " + provider + ", " + status + ", " + extras); } } }, DialerProvider.mLooper); return locationManager.getLastLocation(); }
From source file:net.digitalphantom.app.weatherapp.WeatherActivity.java
private void getWeatherFromCurrentLocation() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION, }, GET_WEATHER_FROM_CURRENT_LOCATION); return;/*from w w w .j a v a 2s . c o m*/ } // system's LocationManager final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); Criteria locationCriteria = new Criteria(); if (isNetworkEnabled) { locationCriteria.setAccuracy(Criteria.ACCURACY_COARSE); } else if (isGPSEnabled) { locationCriteria.setAccuracy(Criteria.ACCURACY_FINE); } locationManager.requestSingleUpdate(locationCriteria, this, null); }
From source file:com.anton.gavel.GavelMain.java
@TargetApi(Build.VERSION_CODES.GINGERBREAD) @Override/* www . j av a 2 s . c o m*/ public void onClick(View arg0) { //click listener for location button LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_HIGH); //String provider = locationManager.getBestProvider(criteria, true); locationManager.requestSingleUpdate(criteria, new LocationListener() { @Override public void onLocationChanged(Location location) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) createDialog(DIALOG_NO_GEOCODING); else if (Geocoder.isPresent()) (new ReverseGeocodingTask(getBaseContext())).execute(new Location[] { location }); // Invoking reverse geocoding in an AsyncTask. } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }, null); }
From source file:com.metinkale.prayerapp.vakit.AddCity.java
@SuppressWarnings("MissingPermission") public void checkLocation() { if (PermissionUtils.get(this).pLocation) { LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location loc = null;//from w w w . j a va 2s.c om List<String> providers = lm.getProviders(true); for (String provider : providers) { Location last = lm.getLastKnownLocation(provider); // one hour==1meter in accuracy if ((last != null) && ((loc == null) || ((last.getAccuracy() - (last.getTime() / (1000 * 60 * 60))) < (loc.getAccuracy() - (loc.getTime() / (1000 * 60 * 60)))))) { loc = last; } } if (loc != null) onLocationChanged(loc); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_MEDIUM); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(false); criteria.setSpeedRequired(false); String provider = lm.getBestProvider(criteria, true); if (provider != null) { lm.requestSingleUpdate(provider, this, null); } } else { PermissionUtils.get(this).needLocation(this); } }
From source file:cubes.compass.service.WeatherActivity.java
private void getWeatherFromCurrentLocation() { // system's LocationManager LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // medium accuracy for weather, good for 100 - 500 meters Criteria locationCriteria = new Criteria(); locationCriteria.setAccuracy(Criteria.ACCURACY_MEDIUM); String provider = locationManager.getBestProvider(locationCriteria, true); // single location update if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return;/*from w ww .j a va2 s . co m*/ } locationManager.requestSingleUpdate(provider, this, null); }
From source file:net.imatruck.betterweather.BetterWeatherExtension.java
/** * Requests a location update if setting is Automatic, else it will give a dummy location * * @param lm Location Manager from {@link net.imatruck.betterweather.BetterWeatherExtension#onUpdateData(int)} * @param provider Provider determined in {@link net.imatruck.betterweather.BetterWeatherExtension#onUpdateData(int)} *//* w w w .ja v a 2 s .c o m*/ private void requestLocationUpdate(final LocationManager lm, final String provider) { if (provider != null && sUseCurrentLocation) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { handleMissingPermission(); return; } final Location lastLocation = lm.getLastKnownLocation(provider); if (lastLocation == null || (SystemClock.elapsedRealtimeNanos() - lastLocation.getElapsedRealtimeNanos()) >= STALE_LOCATION_NANOS) { LOGW(TAG, "Stale or missing last-known location; requesting single coarse location " + "update. " + ((lastLocation != null) ? lastLocation.getLatitude() + ", " + lastLocation.getLongitude() : "Last location is null")); try { disableOneTimeLocationListener(); mOneTimeLocationListenerActive = true; lm.requestSingleUpdate(provider, mOneTimeLocationListener, null); gpsFixHandler.postDelayed(new Runnable() { public void run() { disableOneTimeLocationListener(); LOGD(TAG, "We didn't get a GPS fix quick enough, we'll try again later"); scheduleRefresh(0); } }, 30 * 1000); LOGD(TAG, "Requested single location update"); if (lastLocation != null) { new RefreshWeatherTask(lastLocation).execute(); } } catch (Exception e) { LOGW(TAG, "RuntimeException on requestSingleUpdate. " + e.toString()); scheduleRefresh(2); } } else { new RefreshWeatherTask(lastLocation).execute(); } } else if (!sUseCurrentLocation) { LOGD(TAG, "Using set location"); disableOneTimeLocationListener(); Location dummyLocation = new Location(provider); new RefreshWeatherTask(dummyLocation).execute(); } else { handleMissingPermission(); } }