Java Code Examples for android.location.LocationManager#getLastKnownLocation()
The following examples show how to use
android.location.LocationManager#getLastKnownLocation() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainFragment.java From prayer-times-android with Apache License 2.0 | 7 votes |
@Override public void onResume() { super.onResume(); if (PermissionUtils.get(getActivity()).pLocation) { LocationManager locMan = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); List<String> providers = locMan.getProviders(true); for (String provider : providers) { locMan.requestLocationUpdates(provider, 0, 0, this); Location lastKnownLocation = locMan.getLastKnownLocation(provider); if (lastKnownLocation != null) { calcQiblaAngle(lastKnownLocation); } } } if (Preferences.COMPASS_LAT.get() != 0) { Location loc = new Location("custom"); loc.setLatitude(Preferences.COMPASS_LAT.get()); loc.setLongitude(Preferences.COMPASS_LNG.get()); calcQiblaAngle(loc); } }
Example 2
Source File: Util.java From letv with Apache License 2.0 | 6 votes |
public static String getLocation(Context context) { if (context == null) { return ""; } try { LocationManager locationManager = (LocationManager) context.getSystemService(HOME_RECOMMEND_PARAMETERS.LOCATION); Criteria criteria = new Criteria(); criteria.setCostAllowed(false); criteria.setAccuracy(2); String bestProvider = locationManager.getBestProvider(criteria, true); if (bestProvider != null) { Location lastKnownLocation = locationManager.getLastKnownLocation(bestProvider); if (lastKnownLocation == null) { return ""; } double latitude = lastKnownLocation.getLatitude(); g = latitude + "*" + lastKnownLocation.getLongitude(); return g; } } catch (Throwable e) { f.b("getLocation", "getLocation>>>", e); } return ""; }
Example 3
Source File: LocationUtil.java From mirror with Apache License 2.0 | 6 votes |
@SuppressWarnings("ResourceType") public static boolean isLocationAvailable(final Context context) { final LocationManager manager = (LocationManager) context.getSystemService(LOCATION_SERVICE); if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { Timber.d("NETWORK_PROVIDER is enabled"); manager.removeUpdates(LOCATION_LISTENER); if (manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) == null) { manager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, LOCATION_LISTENER ); } else { return true; } } else { Timber.e("NETWORK_PROVIDER is disabled"); } return false; }
Example 4
Source File: MainActivity.java From Grant with Apache License 2.0 | 6 votes |
private double[] getCoordinates() { LocationManager lm = (LocationManager) getSystemService( Context.LOCATION_SERVICE); List<String> providers = lm.getProviders(true); Location l = null; for (int i = providers.size() - 1; i >= 0; i--) { //noinspection ResourceType l = lm.getLastKnownLocation(providers.get(i)); if (l != null) break; } double[] gps = new double[2]; if (l != null) { gps[0] = l.getLatitude(); gps[1] = l.getLongitude(); } return gps; }
Example 5
Source File: MyLocationActivity.java From Complete-Google-Map-API-Tutorial with Apache License 2.0 | 6 votes |
private void goToMyLocation() { if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { enableMyLocation(); } else { googleMap.setMyLocationEnabled(true); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); if (location != null) { googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13)); } } }
Example 6
Source File: PermissionUtil.java From DoraemonKit with Apache License 2.0 | 6 votes |
/** * 检测是否有定位权限,不可靠,比如在室内打开app,这种情况下定位模块没有值,会报无权限 * * @return */ @SuppressLint("MissingPermission") public static boolean checkLocationUnreliable(Context context) { try { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return false; } Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location location2 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); return location != null || location2 != null; } catch (Exception e) { return false; } }
Example 7
Source File: LocationLogic.java From redalert-android with Apache License 2.0 | 6 votes |
public static Location GetLastKnownLocation(Context context) { // Get location manager LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE); // Create location criteria Criteria criteria = new Criteria(); // Set fine accuracy criteria.setAccuracy(criteria.ACCURACY_FINE); // Get best provider String bestProvider = locationManager.getBestProvider(criteria, false); // No provider? if (bestProvider == null) { // Return null return null; } // Return last location return locationManager.getLastKnownLocation(bestProvider); }
Example 8
Source File: BaseActivity.java From JsBridge with Apache License 2.0 | 6 votes |
public Location getLocation(LocationListener listener) { locationListener = listener; mLocationManager = (LocationManager) HiApplication.getInstance().getSystemService(Context.LOCATION_SERVICE); List<String> providers = mLocationManager.getProviders(true); String locationProvider = null; if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS locationProvider = LocationManager.GPS_PROVIDER; } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是Network locationProvider = LocationManager.NETWORK_PROVIDER; } Location location = mLocationManager.getLastKnownLocation(locationProvider); mLocationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener); return location; }
Example 9
Source File: Address.java From arcusandroid with Apache License 2.0 | 6 votes |
public Location getLastKnownCoarseLocation () { LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE); try { // Walk through each enabled location provider and return the first found, last-known location for (String thisLocProvider : locationManager.getProviders(true)) { Location lastKnown = locationManager.getLastKnownLocation(thisLocProvider); if (lastKnown != null) { return lastKnown; } } } catch(SecurityException exception) { } // Always possible there's no means to determine location return null; }
Example 10
Source File: LocationActivity.java From TelePlus-Android with GNU General Public License v2.0 | 5 votes |
private Location getLastLocation() { LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE); List<String> providers = lm.getProviders(true); Location l = null; for (int i = providers.size() - 1; i >= 0; i--) { l = lm.getLastKnownLocation(providers.get(i)); if (l != null) { break; } } return l; }
Example 11
Source File: GeolocationTracker.java From AndroidChromium with Apache License 2.0 | 5 votes |
/** * Returns the last known location from the network provider or null if none is available. */ static Location getLastKnownLocation(Context context) { if (sUseLocationForTesting) return sLocationForTesting; LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); return locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); }
Example 12
Source File: CbLocationManager.java From PressureNet-SDK with MIT License | 5 votes |
/** * Construct our location manager using network and gps managers, preferences * @param ctx */ public CbLocationManager(Context ctx) { context = ctx; setUpFiles(); try { networkLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); gpsLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); settings = new CbSettingsHandler(context); settings = settings.getSettings(); Location lastKnownNetwork = networkLocationManager.getLastKnownLocation("network"); if(settings.isUseGPS()) { Location lastKnownGPS = gpsLocationManager.getLastKnownLocation("gps"); if(lastKnownGPS != null ) { currentBestLocation = lastKnownGPS; } else { currentBestLocation = lastKnownNetwork; } } else { currentBestLocation = lastKnownNetwork; } } catch(Exception e) { log("cblocation manager error: " + e.getMessage()); e.printStackTrace(); } }
Example 13
Source File: GeolocationTracker.java From 365browser with Apache License 2.0 | 5 votes |
/** * Returns the last known location or null if none is available. * * @param includeGpsFallback Whether the gps provider should also be used as a fallback. * Otherwise only the network provider will be used. */ static Location getLastKnownLocation(Context context, boolean includeGpsFallback) { if (sUseLocationForTesting) { return chooseLocation( sNetworkLocationForTesting, sGpsLocationForTesting, includeGpsFallback); } if (!hasPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)) { // Do not call location manager without permissions return null; } LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); Location networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // If no GPS location has been request, just return the network location. For efficiency, // don't even get the GPS location. if (!includeGpsFallback) { return networkLocation; } Location gpsLocation = null; if (hasPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)) { // Only try to get GPS location when ACCESS_FINE_LOCATION is granted. gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); } return chooseLocation(networkLocation, gpsLocation, includeGpsFallback); }
Example 14
Source File: DeviceLocation.java From ARCore-Location with MIT License | 5 votes |
public DeviceLocation() { try { // Getting LocationManager object locationManager = (LocationManager) LocationScene.mContext.getSystemService(Context.LOCATION_SERVICE); // Creating an empty criteria object Criteria criteria = new Criteria(); // Getting the name of the provider that meets the criteria provider = locationManager.getBestProvider(criteria, false); if (provider != null && !provider.equals("")) { // Get the location from the given provider Location location = locationManager.getLastKnownLocation(provider); locationManager.requestLocationUpdates(provider, 1000, 1, this); if (location != null) onLocationChanged(location); else Toast.makeText(LocationScene.mContext, "Location can't be retrieved", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(LocationScene.mContext, "No Provider Found", Toast.LENGTH_SHORT).show(); } } catch(SecurityException e) { Toast.makeText(LocationScene.mContext, "Enable location permissions from settings", Toast.LENGTH_SHORT).show(); } }
Example 15
Source File: ActionService.java From trojandroid_app with GNU General Public License v3.0 | 5 votes |
private void getLocation(JSONObject argjson) { if (!argjson.has("location")){ return; } //Get location manager LocationManager locManager = (LocationManager) this.context.getSystemService(context.LOCATION_SERVICE); //get the best provider to obtain the current location Location locationNetwork = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); Location locationGPS = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationPassive = locManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); String[] result = new String[6]; //try to get latitude and longitude try { result[0] = "NETWORK Latitude " + String.valueOf(locationNetwork.getLatitude()); result[1] = "NETWORK Longitude " + String.valueOf(locationNetwork.getLongitude()); result[2] = "GPS Latitude " + String.valueOf(locationGPS.getLatitude()); result[3] = "GPS Longitude " + String.valueOf(locationGPS.getLongitude()); result[4] = "PASSIVE Latitude " + String.valueOf(locationPassive.getLatitude()); result[5] = "PASSIVE Longitude " + String.valueOf(locationPassive.getLongitude()); } catch (Exception ex) {//if this failed the method return 0,0 Log.d(TAG, ex.getMessage()); result[0] = "0"; result[1] = "0"; result[2] = "0"; result[3] = "0"; result[4] = "0"; result[5] = "0"; } this.result = new JSONArray(Arrays.asList(result)).toString(); }
Example 16
Source File: LocationActivity.java From TelePlus-Android with GNU General Public License v2.0 | 5 votes |
private Location getLastLocation() { LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE); List<String> providers = lm.getProviders(true); Location l = null; for (int i = providers.size() - 1; i >= 0; i--) { l = lm.getLastKnownLocation(providers.get(i)); if (l != null) { break; } } return l; }
Example 17
Source File: MainFragment.java From AndroidSlidingUpPanel-foursquare-map-demo with Apache License 2.0 | 5 votes |
private LatLng getLastKnownLocation(boolean isMoveMarker) { LocationManager lm = (LocationManager) TheApp.getAppContext().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_LOW); String provider = lm.getBestProvider(criteria, true); if (provider == null) { return null; } Activity activity = getActivity(); if (activity == null) { return null; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (activity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && activity.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return null; } } Location loc = lm.getLastKnownLocation(provider); if (loc != null) { LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude()); if (isMoveMarker) { moveMarker(latLng); } return latLng; } return null; }
Example 18
Source File: BetterWeatherExtension.java From BetterWeather with Apache License 2.0 | 4 votes |
/** * 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)} */ 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(); } }
Example 19
Source File: GPSTracker.java From Women-Safety-App with GNU General Public License v2.0 | 4 votes |
public Location getLocation() { try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { this.canGetLocation = true; if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; }
Example 20
Source File: GPStracker.java From goprohero with MIT License | 4 votes |
public Location getLocation() { try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { this.canGetLocation = true; // First get location from Network Provider if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; }