gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:10:42 25 4
gpt4 key购买 nike

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

我正在两部不同的手机上对此进行测试 - 一部采用定期月费计划的 SGS2 和一部带有预付费 SIM 卡的廉价中兴通讯(有数据,但分钟为 10 美分/分钟)。我发现当我带着两部手机出去兜风时,SGS2 工作得很好,但中兴似乎失去了获得修复的能力。

ZTE 唤醒,设置监听器,获取位置修复,但是位置指向我的房子(它获得最后一个基于 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;
}

最佳答案

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

这是来自 Google Developer Docs 的更好描述.

关于Android:GPS 监听器获取缓存的位置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com