onLocationChanged总是返回我的旧位置

问题描述 投票:9回答:3

我已经每10秒注册一次LocationManager以进行位置更新

mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10 * 1000, 50, this);

但是onLocationChanged回调每10秒返回一个位置,该位置已存在2个小时以上。而且时间戳永远不会改变。

问题是:

两个小时前,我在完全不同的位置(home)上通过wifi使用设备。现在,我现在在另一个wifi上的其他位置(office),我的应用程序将我的当前位置显示为home。昨天在home发生了同样的事情,当时它显示office作为我的当前位置。当我关闭我的应用程序,打开FourSquare应用程序并重新打开我的应用程序时,它开始工作(开始显示正确的位置)。

完整代码:

public class LocationService extends Service implements LocationListener {

    public static double curLat = 0.0;
    public static double curLng = 0.0;
    private LocationManager mgr;
    private String best;
    private Location location;

    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        best = LocationManager.NETWORK_PROVIDER;
        location = mgr.getLastKnownLocation(best);
        if (location != null) {

            dumpLocation(location);
            mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    10 * 1000, 50, this);
        }
        return START_NOT_STICKY;
        }
    }

    private void dumpLocation(Location l) {

        SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy:hh:mm:ss",
                Locale.ENGLISH);
        String format = s.format(l.getTime());
        //The above time is always 28/03/2013:09:26:41 which is more than 2 hrs old
        curLat = l.getLatitude();
        curLng = l.getLongitude();
    }

    @Override
    public void onLocationChanged(Location location) {

        dumpLocation(location);
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}

以这种方式在活动

中开始:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationService.class);
pi = PendingIntent.getService(this, 0, i,
        PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime(), 10000, pi);

清单中的权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

我现在可以通过打开一些其他基于位置的应用程序(例如地图,导航器,Foursquare等,来获取正确的位置,但是为什么我的应用程序无法从提供商处获得新的/最新的修复程序。

谢谢

我已注册我的LocationManager进行位置更新,每10秒钟mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10 * 1000,50,此);但是onLocationChanged回调...

android locationmanager
3个回答
4
投票

由于此行您正在变旧位置


0
投票

我想是因为您立即取消了待处理的意图,因此requestLocationUpdate在取消之前将不会开始更新。为什么您不睡觉可能要等2秒钟才能取消。


0
投票

根据我的经验,即使gps的卫星无法工作,当您请求更新时,android也会为您提供一个位置。因此,即使gps处于打开状态-如果您位于内部或位置较差的地方(例如在桥下),android也会为您提供旧的修复程序。确实可以是很老的。

© www.soinside.com 2019 - 2024. All rights reserved.