安卓:GPS监听器获取缓存的位置数据



我有一段相当简单的代码,它是一个定期运行的服务,记录当前位置(使用网络提供商),将其发送到服务器,然后返回睡眠状态。

我正在两款不同的手机上测试这一点——一款是有常规月度计划的SGS2,另一款是带预付费SIM卡的廉价中兴(有数据,但分钟数为10c/min)。我发现,当我拿着两部手机去开车时,SGS2运行得很好,但中兴似乎失去了修复的能力。

中兴通讯醒来,设置了监听器,得到了一个位置定位,但位置指向我的房子(它得到了最后一个基于wifi的定位),而不是真正的当前位置。位置的时间戳是最新的,所以当我收到位置更新时,我真的不知道位置是有效的(比如在SGS2中,或者中兴在家时)还是无效的(比如我和中兴一起开车时)。

以前有人出现过类似的问题吗?这与预付卡或中兴手机本身有关吗?不幸的是,我无法交换SIM卡(我必须root/解锁手机),所以我无法测试。

我已经包含了下面的代码,但由于它在SGS2上运行良好,我认为没有太大问题。

public class LocationRecorder extends Service {
private volatile Location lastLocation;
private LocationManager locationManager;
private LocationListener locationListener;
private static volatile PowerManager.WakeLock wakeLock = null;

private static synchronized PowerManager.WakeLock getWakeLock(Context context) {
    if (wakeLock == null) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "LocationRecorder");
        wakeLock.acquire();
    }
    return wakeLock;
}

public static void startLocationRecorder(Context context, Intent service) {
    getWakeLock(context);
    context.startService(service);
}

@Override
public void onCreate() {
    Log.d("LocationRecorder", "Starting Location Service");
    locationManager = ((LocationManager)getSystemService(LOCATION_SERVICE));
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d("Location Changed", location.toString());
            if (location.getExtras()!=null) {
                String x = "";
                for (String key : location.getExtras().keySet()) {
                    x+=key+":"+location.getExtras().get(key).toString()+", ";
                }
                Log.d("Location Changed Extras", x);
            }
            setLocation(location);
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d("Status Changed", provider+" "+status);
            if (extras!=null) {
                String x = "";
                for (String key : extras.keySet()) {
                    x+=key+":"+extras.get(key).toString()+", ";
                }
                Log.d("Status Changed Extras", x);
            }
        }
        public void onProviderEnabled(String provider) {
            Log.d("Provider Enabled", provider);
        }
        public void onProviderDisabled(String provider) {
            Log.d("Provider Disabled", provider);
        }
    };
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    waitForLocation();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_REDELIVER_INTENT;
}

@Override
public void onDestroy() {
    locationManager.removeUpdates(locationListener);
    try {
        wakeLock.release();
        wakeLock = null;
    }
    catch (Exception e) {
        wakeLock = null;
    }
    Log.d("LocationRecorder", "Destroying service");
    super.onDestroy();
}

protected void waitForLocation() {
    new Thread() {
        @Override
        public void run() {
            setLocation(null);
            for (int i=0; i<3;i++) {
                Log.d("LocationRecorder", "Waiting for location");
                try {Thread.sleep(10000);} catch(Exception e) {};
                if (getLocation() != null) {
                    Log.d("LocationRecorder", "Sending new location!");
                    new Utilities(LocationRecorder.this).updateLocation(getLocation().getLatitude(),
                                    getLocation().getLongitude(), getLocation().getAccuracy());
                    break;
                }
            }
            stopSelf();
        }
    }.start();
}

public synchronized void setLocation(Location newLocation) {
    lastLocation = newLocation;
}
public synchronized Location getLocation() {
    return lastLocation;
}
@Override
public IBinder onBind(Intent intent) {
    return null;
}

这是预期行为。您可能只需要等待更长的时间,就可以销毁位置管理器,以获得最新的位置。

以下是来自谷歌开发者文档的更好描述。

最新更新