0

我有一个活动,请求 gps_provider,如果禁用,则请求 network_provider。问题是,当 gps 传感器启用,但没有接收到信号(例如在房子里)时,他将获取旧数据(位置不为空)而不是新位置的 network_provider。我可以清除旧的 gps 数据吗?

这里的代码:

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    .....

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location == null) {
        if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location == null) {
            showGPSDisabledAlertToUser();
        }
    }
    if (location != null) {
        this.onLocationChanged(location);
    }


public void onLocationChanged(Location l) {
    locateLandkreis(l);
}

private void locateLandkreis(Location l) {
    new DownloadWarn(this).execute(l); 
}

private class DownloadWarn extends AsyncTask<Location, Integer, String> {

    .....

    @Override
    protected String doInBackground(Location... loc) {

        ......

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.removeUpdates(GPSActivity.this);

        return data;
    }

谢谢奥利弗

4

1 回答 1

1

您应该检查从 GPSProvider 获得的位置的时间,如果它比某个特定阈值更早,也请查看 NetworkProvider 的位置。

所以,而不是

if (location == null) { ...

做这个

if (location == null || 
    System.currentTimeMillis()-location.getTime() > THRESHOLD) { ...

THRESHOLD以毫秒为单位的阈值在哪里。

于 2013-05-02T11:21:00.140 回答