Android 是否有及时获取最新修复(而不是最准确的修复)的标准

Android 是否有及时获取最新修复(而不是最准确的修复)的标准,android,gps,locationmanager,Android,Gps,Locationmanager,这是我用来获取最后一个已知位置而不添加侦听器的代码。我不想耗尽电池电量,所以我使用: public static Location getLastLoc(Context context){ Location loc = null; LocationManager locationManager = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); Cri

这是我用来获取最后一个已知位置而不添加侦听器的代码。我不想耗尽电池电量,所以我使用:

public static Location getLastLoc(Context context){

    Location loc = null;

    LocationManager locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String best = locationManager.getBestProvider(criteria, true);
    //Log.i("***", best);
    if (best != null) {
        loc = locationManager.getLastKnownLocation(best);
    }
    //Sometimes getLastKnownLocation return null (new device), so I use network as default when possible. 
    if (loc == null) {
        try {
            loc = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return loc;
}
我目前正在尝试这个代码和最好的供应商听起来是全球定位系统

不幸的是,今天早上我从A市搬到了B市,还没有GPS定位。(但GPS已打开)

因此,当我在城市B时,我仍然会得到老城市A,网络位置知道城市B(通过谷歌地图测试)

因此,由于我不需要精确的位置,如何才能及时获得最新的定位(GPS已使用3小时,网络已使用10分钟)


谢谢

您必须利用侦听器来获取最后知道的位置。因为

          locationManager.getLastKnownLocation(best);

它不会调用GPS以获取更新的位置。此方法仅向locationManager对象提供最后一个已知位置。这就是为什么您的位置没有更新。

在等待更好的答案时,我唯一能做的就是检查最新的已知位置是否不超过一小时:

loc = locationManager.getLastKnownLocation(best);
if ((System.currentTimeMillis() - loc.getTime()) < DateUtils.HOUR_IN_MILLIS)
    return loc;

try {
    if (locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) != null)
        loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    } catch (Exception e) {
        e.printStackTrace();
    }
loc=locationManager.getLastKnownLocation(最佳);
if((System.currentTimeMillis()-loc.getTime())
是,它将调用GPS对象并返回GPS的上一次定位。我不想打开GPS。如果相关,我想使用最后知道的位置