2012-03-26 55 views
4

裏面我讀了很多的問題,在這裏,但無法弄清楚是什麼問題。與AlarmManager的Android定期GPS位置更新服務

我正在寫爲Android現場服務應用程序。在其中一個活動(MyActivity.java)中,我有兩個按鈕:開始和停止。

當現場工作人員按下啓動,我需要與GPS讓他當前的位置,並將其發送到及時區間服務器(說爲5分鐘默認情況下,在這裏我將它設置爲20秒。用於測試)。客戶希望看到工人在交通上花費多少時間,我的城市(伊斯坦布爾)的交通情況很糟糕。

我在我的活動中有一個AlarmManager,當按下開始按鈕時,報警設置爲AlarmManager.setRepeating()以啓動服務(ServiceClass.java)。

Intent intent = new Intent (MyActivity.this, ServiceClass.class); 
mPendingIntent = PendingIntent.getService(MyActivity.this, 0, intent, 
               PendingIntent.FLAG_CANCEL_CURRENT); 
mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
mAlarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 20*1000, 
                    mPendingIntent); 

我用停止按鈕取消報警。

它完美到這裏,在服務啓動。它的onCreate()onStart()onDestroy()方法被執行。但我無法獲得位置。我沒有在真正的設備上工作,所以我使用DDMS發送位置信息。但是該應用程序跳過該步驟並將我的位置的經度和緯度打印爲Lon:0緯度:0。

這裏的服務裏面的代碼:

public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 
    Log.d("Testing", "Service got started, calling location updates:"); 
    mLocationManager.requestSingleUpdate(mCriteria, mLocationListener, 
                   getMainLooper()); 
    Log.i("TESTING LOCATION UPDATE: LOCATION:", "\nLat:" + mLatitude + 
                  " Lon:" + mLongitude); 
    stopSelf(startId); 
    Log.d("Testing", "Service Stopped!"); 

最後,這裏是我的LocationListener的:

mLocationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 

      if(location != null){ 
       mLatitude = location.getLatitude(); 
       mLongitude = location.getLongitude(); 
       Log.i("onLocationChanged(Location location)", "Lat:"+mLatitude + " 
                  Lon:" + mLongitude); 
} 

也是東西引起了我的眼睛,當我運行代碼:

mLocationProvider = mLocationManager.getBestProvider(mCriteria, true); 
Log.i("BEST PROVIDER IS:", mLocationProvider); 

它說最好的供應商是:gps。但是這個日誌裏面onProviderEnabled()從來沒有顯示在logcat中。

@Override 
public void onProviderEnabled(String s) { 
    Log.v("onProviderEnabled", "ENABLED"); 
} 

兩件事情的添加,如果我使用:

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
                   mLocationListener); 

它也不起作用。

然而,這工作,我可以得到LastKnownLocation:

Location lastKnown = mLocationManager.getLastKnownLocation(
                LocationManager.GPS_PROVIDER); 

首先,這是這樣做的一個好方法?如果是這樣,請檢查我的缺失。如果不是,你能告訴我怎樣才能更好地實現這一點?

謝謝。 :)

+1

在Android的位置更好的文章之一; http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html - 它解決了你的問題 – tonys 2012-03-26 09:55:21

+0

@tonys感謝東尼斯最,我會讀的是第一件事情。 – Lev 2012-03-26 12:47:28

+0

這完美的作品:[請在此輸入鏈接的描述(http://stackoverflow.com/questions/21762293/run-gps-as-background-service-and-send-coordinates-to-web-server-php) – 2016-12-18 08:57:53

回答

8

首先,你不能註冊爲位置更新,然後關閉該服務。充其量,你會泄漏線程。最糟糕的情況是,Android會終止你的程序(認爲它沒有任何運行),你不會得到更新。此外,GPS需要一段時間來預熱,所以在20秒內可能無法修復,特別是在城市環境中。另外,當您嘗試收集修復程序時,您必須確保設備保持清醒狀態。

其結果是,在後臺獲取週期性位置修復是一個相當複雜的問題。

我寫了一個現在已經停產LocationPoller試圖解決這個問題,並another developer has forked and extended it。你可以嘗試一下fork,或者簡單地把它當作想法的來源。

+0

10幾分鐘後,我發佈了這個我偶然發現了你的LocationPoller。在我下載並查看源代碼之前,電力已經熄滅。 :) 非常感謝。 – Lev 2012-03-26 12:49:08

+0

請問'requestLocationUpdates(字符串提供商,長minTime,浮minDistance纔會,意圖的PendingIntent)'與清單登記接收器從有同樣的問題?也就是說,'AlarmManager'是用來傳遞PendingIntent(在這種情況下,接收器將被喚醒)? – 2013-05-10 00:33:34

+0

@Mr_and_Mrs_D:對不起,但我不明白你的問題。 – CommonsWare 2013-05-10 01:01:32

1

試試這個:

AlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,,,); 

我做了一個獲取的當前位置與GPS,但我的項目每10秒的作品。我現在不知道。和我的代碼:

AlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000000,pi);