I'm trying to use FusedLocation API in a Background service. I'm getting error on :-
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleAPIClient, mLocationRequest, this);
asking me to cast the 3rd parameter into (cast to com.google.android.gms.location.LocationListener). I tried to do so and App crashed.I don't have any Idea why this happening or asking to to cast.
full code for the service class is as below:-
public class LocationBgService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleApiClient mGoogleAPIClient;
private LocationRequest mLocationRequest;
private FusedLocationProviderApi mLocationProvider;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mGoogleAPIClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mLocationProvider = LocationServices.FusedLocationApi;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(60000);
mLocationRequest.setFastestInterval(15000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mGoogleAPIClient.connect();
return START_STICKY;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
RequestLocationUpdates();
}
private void RequestLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleAPIClient, mLocationRequest, this); //getting error here..for casting..!
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
Log.d("Background Location ", "::::***********Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
I searched and found many people are using this same line of code,so why I'm getting this error. Did I missed something to do or what? please help me with this.Thanks in advance.
You're probably using the older form of the LocationListener. It looks like you're using the one intended for LocationManager rather than the newer LocationServices API.
In your import statements, change
import android.location.LocationListener;
To this
import com.google.android.gms.location.LocationListener;
You'll need to remove the 3 methods at the bottom. They're not used in the newer LocationListener. Also, you may need to clean & do a complete rebuild. Using the new import statement, your code works for me.