如何使Last LatLong使用FusedLocationProviderClient进入服务?

时间:2018-07-28 13:42:32

标签: android google-play-services latitude-longitude google-location-services

我正在使用此代码获取最新的纬度-

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();
            }
        }
    });

此代码在片段或活动中均能正常工作,但在添加服务时会显示错误,如下图所示:

enter image description here

我想在服务中添加此方法,任何人都可以帮助我...

1 个答案:

答案 0 :(得分:1)

尝试在addOnSuccessLitener函数中删除AutoMessagingService.this。

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();
                }
            });