android - 如何让 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();
}
}
});
此代码在片段或活动中运行良好,但在添加服务时显示如下图所示错误:
我想将此方法添加到服务中,任何人都可以帮助我...
解决方案
尝试在 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();
}
});
推荐阅读
- android - 使用 CameraX 启动和停止相机的正确方法是什么?
- rust - 什么时候可以安全地将成员价值移出固定的未来?
- angularjs - 如果值为是,则检查角度复选框
- postgresql - 当 TIMESTAMP 列的值为 NULL 时,如何使用 COALESCE 返回“N/A”?
- redis - 带有 redis 的 phpspreadsheet 增加了运行时间
- python - Django filter manytomanyfield 排除与另一个模型相关的对象
- python - 是否可以将 python pandas 中的 groupby 应用于已经分组的对象?
- javascript - 检索使用 innerHTML 创建的按钮的名称
- android - android.content.Intent 在哪个进程下运行?
- angular - 将静态元素设置为下拉列表中的最后一个?