2010-10-22 140 views
5

我有問題,我的服務,實現了LocationListener,每500毫秒似乎收到一個GPSupdate。無論我把什麼minTime放在requestLocationUpdates函數中。requestLocationUpdates不會更新間隔Android

我的一段代碼:

public class LocationService extends Service implements LocationListener { 

    LocationManager locMan; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     locMan.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER; 
     locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 1, this); 

    } 

    public void onLocationChanged(Location location) { 
     Log.d("Loc", "Location has been changed"); 
    } 
} 

從一個按鈕,在我的主要活動我會打電話給startService(),在此之後它應該在後臺運行,但它提供了持續的更新。

回答

1

我看到了一些東西不對您的情況:

GPS的精確度是最好的,在3米左右,這是在最佳條件下。所以如果位置改變1米,請求更新是沒有意義的。當設備靜止不動時,GPS噪音很容易指示移動。使minDistance至少10是我的建議。

我不明白你爲什麼要調用isProviderEnabled(),因爲你沒有對返回值做任何事情。

你說startService()後應該在後臺運行,但事實並非如此。除非您爲後臺創建某種後臺線程,否則服務不會在後臺運行。這個答案中有太多內容要解釋服務如何工作。上網並做一些閱讀,或者獲得一本關於Android開發的好書。