Mahmudur Rahman
Mahmudur Rahman

Reputation: 639

How to get Last LatLong use FusedLocationProviderClient into the service?

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:

enter image description here

I want to add this method in service, anyone help me...

Upvotes: 0

Views: 329

Answers (1)

Yang
Yang

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

Related Questions