🌐
Android Developers
developer.android.com › api reference › locationmanager
LocationManager | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Stack Overflow
stackoverflow.com › questions › 23886153 › android-gps-disable-updates
Android Gps Disable Updates - Stack Overflow
You can disable updates for a specific listener by calling removeUpdates method of LocationManager class. public class AndroidGPSSampleActivity extends Activity { private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; private static ...
🌐
Google
developers.google.com › google play services › fusedlocationproviderclient
FusedLocationProviderClient | Google Play services | Google for Developers
October 31, 2024 - The getCurrentLocation(CurrentLocationRequest, CancellationToken) API is designed with exactly this use case in mind. On the other hand, if repeated location updates are required, such as when tracking the user's location over time, requestLocationUpdates(LocationRequest, Executor, LocationListener) or one of its variants is better suited.
🌐
Stack Overflow
stackoverflow.com › questions › 40084896 › how-to-enable-and-disable-gps-updateslocationmanager-requestlocationupdates
How to enable and disable GPS updates(LocationManager.requestLocationUpdates(...)) based on a condition in Android application - Stack Overflow
//Intent service code public class MyLocationService extends IntentService { protected int runningLocationScan; GPSTracker GPSobjLM; public MyLocationService() { super("myLocationService-thread"); } @Override protected void onHandleIntent(Intent intent) { Log.d("SERVICE", "ON HANDLE INTENT"); } @Override public void onCreate() { super.onCreate(); GPSobjLM = new GPSTracker(this); if(GPSobjLM.isGPSEnabled) runningLocationScan = 1; else runningLocationScan = 0; Log.d("SERVICE", "ONCREATE"); } @Override public void onDestroy() { super.onDestroy(); if(GPSobjLM.isGPSEnabled) GPSobjLM.stopUsingGPS();
🌐
Google
developers.google.com › google play services › fusedlocationproviderapi
FusedLocationProviderApi | Google Play services | Google for Developers
Updates can be started for a `LocationListener`, `LocationCallback`, or `PendingIntent` with `requestLocationUpdates` and canceled using `removeLocationUpdates`. Batched locations are sent via `flushLocations`. Mock locations can be set with ...
🌐
Android Developers
developer.android.com › core areas › sensors and location › request location updates
Request location updates | Sensors and location | Android Developers
This document explains how to request regular updates about a device's location using the Fused Location Provider's requestLocationUpdates() method in Android.
🌐
DEV Community
dev.to › olubunmialegbeleye › location-services-the-android-14-maybe-15-too-way-4171
Location Services- the Android 14 (maybe 15 too) way - DEV Community
July 16, 2024 - @SuppressLint("MissingPermission") private fun requestLocationUpdate(activity: Activity) { if (isLocationPermissionGranted(activity)) { mFusedLocationProviderClient.requestLocationUpdates( locationRequest, locationCallBack, Looper.getMainLooper() ) } }
Find elsewhere
🌐
Medium
medium.com › @boobalaninfo › accessing-users-location-guide-android-2023-60a6f018a718
Accessing User’s Location Guide Android 2023 | by Boobalan Munusamy | Medium
June 22, 2023 - class PiLocationException (message:String):Exception() @Singleton class PiLocationManager @Inject constructor(@ApplicationContext val context: Context) { private val fusedLocationClient: FusedLocationProviderClient by lazy { LocationServices.getFusedLocationProviderClient(context) } @SuppressLint("MissingPermission") fun locationUpdates(intervalInMillis: Long): Flow<Location> { return callbackFlow { if (!context.checkLocationPermission()) { throw PiLocationException(context.getString(R.string.missing_location_permission)) } if (!context.isNetworkOrGPSEnabled()) { throw PiLocationException(cont
Top answer
1 of 2
40

You are implementing LocationListener in your activity MainActivity. The call for concurrent location updates will therefor be like this:

mLocationClient.requestLocationUpdates(mLocationRequest, this);

Be sure that the LocationListener you're implementing is from the google api, that is import this:

import com.google.android.gms.location.LocationListener;

and not this:

import android.location.LocationListener;

and it should work just fine.

It's also important that the LocationClient really is connected before you do this. I suggest you don't call it in the onCreate or onStart methods, but in onResume. It is all explained quite well in the tutorial for Google Location Api: https://developer.android.com/training/location/index.html

2 of 2
13

I use this one:

LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

For example, using a 1s interval:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);

the time is in milliseconds, the distance is in meters.

This automatically calls:

public void onLocationChanged(Location location) {
    //Code here, location.getAccuracy(), location.getLongitude() etc...
}

I also had these included in the script but didnt actually use them:

public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}

In short:

public class GPSClass implements LocationListener {

    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
    }
}
🌐
Narkive
android-developers.narkive.com › YTbYlJyZ › locationmanager-requestlocationupdates
LocationManager.requestLocationUpdates()
Post by Lance Nanek Neither version blocks. The onLocationChanged method won't even be called during the requestLocationUpdates call, only afterwards as a separate event.
🌐
Vanderbilt
dre.vanderbilt.edu › ~schmidt › android › android-4.0 › out › target › common › docs › doc-comment-check › reference › android › location › LocationManager.html
LocationManager | Android Developers
In case the provider is disabled by the user, updates will stop, and an intent will be sent with an extra with key KEY_PROVIDER_ENABLED and a boolean value of false.
🌐
Medium
medium.com › @huseyin_37353 › best-way-to-get-location-on-android-72695fef17a4
Best and Trustworthy Way to Get Location on Android | by Hüseyin Bülbül | Medium
October 16, 2020 - { locationResult?.lastLocation?.let { haveNewLocation(it) } } } locationRequest = LocationRequest() locationRequest?.apply { priority = LocationRequest.PRIORITY_HIGH_ACCURACY interval = 5000 } client?.let { val hasFineLocationPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) val hasCoarseLocationPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) if(hasFineLocationPermission == PackageManager.PERMISSION_GRANTED && hasCoarseLocationPermission == PackageManager.PERMISSION_GRANTED) { it.requestLocationUpdates(locationRequest, callback, null) } } }
🌐
Android Developers
developer.android.com › core areas › sensors and location › get the last known location
Get the last known location | Sensors and location | Android Developers
Location is turned off in the device settings. The result could be null even if the last location was previously retrieved because disabling location also clears the cache.
🌐
Google
developers.google.com › glass explorer edition › locations and sensors
Locations and Sensors | Glass Explorer Edition | Google for Developers
Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(true); List<String> providers = locationManager.getProviders( criteria, true /* enabledOnly */); for (String provider : providers) { locationManager.requestLocationUpdates(provider, minTime, minDistance, listener); } Glass has a specialized sensor to detect whether or not the device is on the users' head. When enabled, this setting helps conserve battery when the device is not in use. You can use this feature in your Glassware to disable or throttle background services.