2012-03-14 80 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

相關問題