2012-03-14 79 views
1

我有一段相当直接的代码,它是一个定期运行的服务,记录当前位置(使用网络提供者),将其发送到服务器,然后返回到睡眠状态。Android:GPS侦听器获取缓存的位置数据

我在两款不同的手机上测试了这款手机 - 一款每月定期计划的SGS2和一款带有预付费SIM卡(有数据,但分钟数为10c/min)的廉价ZTE。我发现,当我拿两部手机和一部硬盘时,SGS2的工作状况非常好,但中兴似乎失去了修复的能力。

中兴通讯唤醒了,建立了听众,得到了定位,但是该位置指向了我的家(它获得了最后一个基于wifi的定位),而不是真正的当前位置。位置的时间戳是最新的,所以当我收到位置更新时,我确实无法分辨位置是否有效(如在SGS2中,或者当ZTE在家时)或者是双层的(比如当我与中兴通讯一起开车)。

以前有没有人看起来类似的问题?它与预付卡或中兴手机本身有什么关系?不幸的是,我不能交换SIM卡(我必须根/解锁手机),所以我无法测试。

我已经包含了下面的代码,但由于它在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; 
} 

回答

1

这是预期的行为。在摧毁位置经理以获取更新位置之前,您可能需要等待更长的时间。

这是从Google Developer Docs的更好的描述。

+2

这很有道理,但如何判断我收到的修复程序是否真的老了?我无法从检查位置对象(包括其附加内容)中找到它,因为位置时间戳显示为最新的,而附加信息指示它是来自“wifi”还是“cell”,networkLocationSource总是被“缓存”。 – Martin 2012-03-14 22:53:18

+0

你不应该从它的额外获得'Location'对象的时间,而是调用[location.getTime()](http://developer.android.com/reference/android/location/Location.html#getTime() )。这将确保您获得确切的修复时间,即使其缓存/旧。 – 2014-04-25 08:02:28

相关问题