Reputation: 639
I am using this code in service to get last Latitude Longitude -
FusedLocationProviderClient mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(AutoMessagingService.this);
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(AutoMessagingService.this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
lLatitude = location.getLatitude();
lLongitude = location.getLongitude();
}
}
});
This code works fine in fragment or activity but when add in service it show error like below image:
I want to add this method in service, anyone help me...
Upvotes: 0
Views: 329
Reputation: 11
try to remove AutoMessagingService.this in addOnSuccessLitener function.
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// GPS location can be null if GPS is switched off
if (location != null) {
lLatitude = location.getLatitude();
lLongitude = location.getLongitude();
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
Upvotes: 1