phone 上的位置 return 为空
Location return null on the phone
我想查看我当前的经纬度。此代码在模拟器上运行,但当我检查 phone 时,我有空值。(android 10)
phone 上的应用程序具有位置权限。
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location!=null){
MyLong=location.getLongitude();
MyLat=location.getLatitude();
test.setText(MyLat+" " +MyLong);
}
else{
test.setText("null");
}
}
});
基于 official android documentation,在某些情况下最后已知的位置可能为空,这些是
- 设备设置中的定位功能已关闭。结果可能是
null 即使之前检索到最后一个位置,因为
禁用位置也会清除缓存。
- 设备从未记录过它的位置,这可能是
新设备或已恢复出厂设置的设备。
- Google设备上的播放服务已经重启,没有
已请求位置的活动 Fused Location Provider 客户端
服务重启后
在那些情况下你应该request location updates. You could check the example of Google's Codelabs
我想查看我当前的经纬度。此代码在模拟器上运行,但当我检查 phone 时,我有空值。(android 10)
phone 上的应用程序具有位置权限。
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location!=null){
MyLong=location.getLongitude();
MyLat=location.getLatitude();
test.setText(MyLat+" " +MyLong);
}
else{
test.setText("null");
}
}
});
基于 official android documentation,在某些情况下最后已知的位置可能为空,这些是
- 设备设置中的定位功能已关闭。结果可能是 null 即使之前检索到最后一个位置,因为 禁用位置也会清除缓存。
- 设备从未记录过它的位置,这可能是 新设备或已恢复出厂设置的设备。
- Google设备上的播放服务已经重启,没有 已请求位置的活动 Fused Location Provider 客户端 服务重启后
在那些情况下你应该request location updates. You could check the example of Google's Codelabs