Amin Pinjari
Amin Pinjari

Reputation: 2173

requestSingleUpdate not working on Oreo

I have developed an app which sends me location for single time, Everything was working fine but I am not receiving locations in Oreo device.Location permission is provided and GPS is also turned on

Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                Log.d(TAG, "requestSingleUpdate: ");
                locationManager.requestSingleUpdate(criteria, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                        callback.onSomethingWrong("Status Changed" + status);
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                        callback.onSomethingWrong("Provider Enabled " + provider);
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                        callback.onSomethingWrong("Provider Disabled" + provider);
                    }
                }, null);

Upvotes: 1

Views: 3036

Answers (3)

CoolMind
CoolMind

Reputation: 28845

requestSingleUpdate was deprecated in API 30.

Use getCurrentLocation(java.lang.String, android.os.CancellationSignal, java.util.concurrent.Executor, java.util.function.Consumer) instead as it does not carry a risk of extreme battery drain.

Looking at https://medium.com/@priyavrat.acharya01/using-new-on-demand-location-update-api-introduced-in-android-11-69c65b3787aa we can see:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    // Fetching the one time current location.
    val locationManager: LocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager

    // Handle runtime location permission, before calling this method, otherwise it will through the SecurityException.
    // Provider, executor and Consumer<Location> cannot be passed null.
    // CancelationRequest object can be passed as null.          
    locationManager.getCurrentLocation(LocationManager.NETWORK_PROVIDER, null, this.mainExecutor, locationCallback)
} else {
    // Keep using the legacy code, such as LocationManager.requestSingleUpdate()
}

private val locationCallback = Consumer<Location> { location ->
    location?.let {
        Timber.d("Latitude: ${it.latitude.toString()}, longitude: ${it.longitude.toString()}")
    }
}

I didn't have problems with requestSingleUpdate on Oreo. I didn't test the code, because also migrated to FusedLocationProviderClient. getCurrentLocation won't work before API 30.

Upvotes: 3

GigiManco
GigiManco

Reputation: 67

LocationManager APIs, like requestSingleUpdate, are obsolete for Oreo: you have to use FusedLocationProviderClient instead.

You can refer to google developer tutorials. Particularly, for migration: Migrate to location and context APIs

My tests confirm that the fix is backwards-compatible (tested also with Android 6)

Upvotes: 1

EsbenG
EsbenG

Reputation: 28162

Are you sure you have permissions? Location is considered a dangerous permission and as such should be requested at run time. See more here:

https://developer.android.com/guide/topics/permissions/overview#normal-dangerous

Upvotes: -1

Related Questions