2012-02-16 85 views
2

我有一個奇怪的問題,花了很多分鐘,現在試圖找出這一點。對於Android Market中的我的NFC Checkin應用程序,我正在訪問用戶的位置以進行Foursquare搜索。Android LocationListener.onLocationChanged從來沒有叫

所以,這裏的大概我做什麼(類似的代碼已經工作過 - 對AA的Galaxy Nexus,但現在我仍然在Android 4.0.2):

  Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_MEDIUM); 

     String providerName = locationManager.getBestProvider(criteria, true); 


     Location loc = locationManager.getLastKnownLocation(providerName); 

     showSearchUI(); 
     if (loc.getTime() > (new Date().getTime() - 1000*2*60)) //2Minutes back 
     { 
      Log.d("demo", "LastKnownLocation is good enough, using this location!"); 
      startVenueSearch(loc); 
     } 
     else 
     { 
      Log.d("demo", "Requesting network location updates..."); 
      locationManager.requestLocationUpdates(providerName, 0, 0, this); 
     } 

活動實現了LocationListener的接口,所以這裏是onLocationChanged方法:

@Override 
public void onLocationChanged(Location location) { 
    locationManager.removeUpdates(this); 
    Log.d("demo", "Got fresh location, beginning... " + location.getLatitude() + "/" + location.getLongitude()); 
    //fake hybris 
    //location.setLatitude(48.150098d); 
    //location.setLongitude(11.546026d); 

    startVenueSearch(location); 

} 

問題:我只是得不到位置更新。我只是繞過代碼,將代碼從直接調用NetworkProvider改爲基於標準的搜索提供程序,但它仍然不起作用。任何人?

+0

您是否找到解決方案? – 4gus71n 2013-11-22 13:36:29

回答

0

如果您每次不會調用requestLocationUpdates時都碰到第一個if條件。這是註冊您的監聽器並導致您獲取位置更新的內容。確保爲這兩種方法的更新註冊或只是提取它,所以它被調用無論哪個條件滿足

2

@Override可能是一個問題,因爲LocationListener現在是一個接口。嘗試使用

public class MyLocationListener implements LocationListener{ 
    public void onLocationChanged(Location location) { 
     Toast("onLocationChanged"); 
    } 

    public void onProviderDisabled(String provider) { 
     Toast("onProviderDisabled"); 
    } 

    public void onProviderEnabled(String provider) { 
     Toast("onProviderEnabled");   
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
     Toast("onStatusChanged"); 
    }  
}