1

How to fix deprecation warning exactly for requestLocationUpdates.

Android LocationServices.FusedLocationApi deprecated has the answers for it. But I can't understand how to fix it

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(DriverMapActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},LOCATION_REQUEST_CODE  );
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);




}
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
shayanmalinda
  • 1,925
  • 3
  • 12
  • 23

3 Answers3

1

You need to use FusedLocationProverClient

private FusedLocationProviderClient fusedLocationClient;

In your onCreate method initialize the fusedLocationClient

fusedLocationClient = LocationServices.getFusedLocationProviderClient(Activity.this);

To request location updates

fusedLocationClient.requestLocationUpdates(locationRequest,
                locationCallback,
                null /* Looper */);

Hope it helps!

Ahmad Sabeh
  • 526
  • 5
  • 18
0

FusedLocationProviderApi has been deprecated for long which is why the official guide suggests to usee FusedLocationProviderClient. Just call startLocationUpdates() to get the updates like this:

override fun onResume() {
    super.onResume()
    if (requestingLocationUpdates) startLocationUpdates()
}

private fun startLocationUpdates() {
    fusedLocationClient.requestLocationUpdates(locationRequest,
            locationCallback,
            null /* Looper */)
}
SaadAAkash
  • 3,065
  • 3
  • 19
  • 25
0

Use the FusedLocationProviderClient class. This is the one google wants us to use.

I'm providing you with a short sample.

private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest locationRequest;
private LocationCallbackImpl mLocationCallback = null;
...
if (mFusedLocationClient == null) {

            locationRequest = new LocationRequest()
                    .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                    .setInterval(1000)
                    .setFastestInterval(1000);

            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
            builder.addLocationRequest(locationRequest);

            mFusedLocationClient = LocationServices.getFusedLocationProviderClient(activity);
            mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.myLooper());

}

/**
  * Callback class
  */
private class LocationCallbackImpl extends LocationCallback {

    private Activity activity;

    public LocationCallbackImpl(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onLocationResult(LocationResult locationResult) {
    }

}
March3April4
  • 2,131
  • 1
  • 18
  • 37