首页 > 解决方案 > 如何让 Last LatLong 使用 FusedLocationProviderClient 进入服务?

问题描述

我在服务中使用此代码来获取最后一个纬度经度 -

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

此代码在片段或活动中运行良好,但在添加服务时显示如下图所示错误:

在此处输入图像描述

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

标签: androidgoogle-play-serviceslatitude-longitudegoogle-location-services

解决方案


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

推荐阅读