2013-04-24 48 views
0

我正在開發一個應用程序需要檢索用戶位置的時間段。我在活動中使用了LocationListener接口,並且它運行良好,但是當我在我的android服務中實現它時,它不會被調用。這裏的onCreate方法:onLocationUpdate不是在服務調用

public void onCreate() { 
    super.onCreate(); 
    turned= true;  
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    locationManager = (LocationManager) this 
      .getSystemService(LOCATION_SERVICE); 
    String provider = locationManager.getBestProvider(criteria, true); 
    locationManager 
      .requestLocationUpdates(provider, 0, 0, this); ...} 

我真的很感謝所有幫助,服務優良似乎因爲我送麪包和它們被髮送,但onLocationUpdate不會被調用。

+0

你確定它實際上有一個GPS鎖?這可能需要一段時間。參考:http://stackoverflow.com/questions/5337333/android-onlocationupdate-not-called-with-gps-provider – 2013-04-24 17:33:55

+0

我打電話給onStartCommand()而不是OnCreate(),它適用於我。您可能想嘗試一下。 – 2013-04-24 17:36:10

+0

非常感謝Shobhit Puri,你的回答解決了我的問題。 – user1998791 2013-04-24 19:26:59

回答

0

編輯:由於Shobhit普里說,它只是需要在onStartCommand()

public int onStartCommand (Intent intent, int flags, int startId) 
{ 
    super.onCreate(); 
    encendido = true;  
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    locationManager = (LocationManager) this 
      .getSystemService(LOCATION_SERVICE); 
    String provider = locationManager.getBestProvider(criteria, true); 
    locationManager 
      .requestLocationUpdates(provider, 0, 0, this); 
    return START_STICKY; 
}