我正在使用此代码获取最新的纬度-
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();
}
}
});
此代码在片段或活动中均能正常工作,但在添加服务时会显示错误,如下图所示:
我想在服务中添加此方法,任何人都可以帮助我...
答案 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();
}
});