即使在请求更新之后,也永远不会调用onLocationChanged

时间:2013-03-29 07:00:40

标签: android

启动服务:

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(), 30*1000, pi);

服务

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.NETWORK_PROVIDER,
                    5 * 1000, 0, 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 21/03/2013:09:53:41 which is more than a week old
        curLat = l.getLatitude();
        curLng = l.getLongitude();
    }

    @Override
    public void onLocationChanged(Location location) {

        //This never seems to be called
        dumpLocation(location);
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

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

    }
}

每30秒调用一次服务,但requestLocationUpdates似乎永远不会调用onLocationChanged。当我检查lastKnownLocation一周前的时间时。

foursquare,本地,地图等应用程序会在当前位置禁用GPS。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我认为您需要使用您的设备从某处移动,因为只有在位置发生变化时才调用onLocationChange。并且网络位置接近1000-2000米,因此需要检查位置变化的街道并获取新位置。如果你使用gps那么它会给你准确但在修复它之后它也在20-30米附近。