2014-04-01 77 views
0

我使用PendingIntent調用requestLocationUpdates。後來我想知道這個請求是否仍然有效。我怎樣才能做到這一點?檢查Android應用程序LocationClient是否請求位置更新

我知道我是否已經調用removeLocationUpdates,但我希望可以有其他方式位置更新可以停止,我不想錯。

這個問題不是關於geofences。

回答

1

據:

http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html

如果您正在尋找地理圍欄:

static List<Geofence> getTriggeringGeofences(Intent intent) 
Returns a list of geofences that triggers this geofence transition alert. 

否則:

不,你不能在一個通用的方式問。這就是說,如果你有一個監聽器,有興趣的幾種方法:

boolean isConnectionCallbacksRegistered(GooglePlayServicesClient.ConnectionCallbacks listener) 
Returns true if the specified listener is currently registered to receive connection events. 


    boolean isConnectionFailedListenerRegistered(GooglePlayServicesClient.OnConnectionFailedListener listener) 
    Returns true if the specified listener is currently registered to receive connection failed events. 

    void  registerConnectionCallbacks(GooglePlayServicesClient.ConnectionCallbacks listener) 
    Registers a listener to receive connection events from this GooglePlayServicesClient. 

    void  registerConnectionFailedListener(GooglePlayServicesClient.OnConnectionFailedListener listener) 

    Registers a listener to receive connection failed events from this GooglePlayServicesClient. 

所有這些都會告訴你關於你的具體聽衆。我沒有看到任何代碼能夠一般地回答「你有沒有人聽你說過什麼」的問題?

你很可能自省,迫使API做一些它的內部可見,但你也許可以完成你想要一些其他不太難看,這樣的東西。

你想完成什麼?

EDITS:

從文檔,以及:

公共無效requestLocationUpdates(LocationRequest請求, 的PendingIntent callbackIntent)

依指定 的PendingIntent回調請求位置更新。

這種方法適用於後臺使用情況,更具體地說是用於接收位置更新的 ,即使該應用程序已被系統中的 殺死。爲此,請使用PendingIntent作爲啓動的 服務。對於前臺使用情況,建議使用 方法的LocationListener版本,請參閱requestLocationUpdates(LocationRequest, LocationListener)。

在此的PendingIntent註冊任何以前LocationRequests將 取代。

這是什麼意思?

1)您的請求上演,並不會不分階段,直到你明確地調用

public void removeLocationUpdates (PendingIntent callbackIntent) 

2)這是真實的,即使你的應用程序已經被系統殺死。 (假設背景使用情況。我不知道在前臺使用情況)。

3)如果LocationClient已停止接收更新 - 您的意圖將不會被通知。你將不得不撥打:

isConnected()檢查客戶端當前連接到 服務,使請求其他方法會成功。 boolean
isConnecting()檢查客戶端是否嘗試連接到 服務。

位置更新通過KEY_LOCATION_CHANGED的密鑰和 意圖的位置值發送。

參數請求更新的位置請求。 callbackIntent每個位置更新的待發送意圖。

因此,後來的點 - 我會檢查LocationClient是否連接/連接。如果不是,那麼假設你的意圖不會被解僱 - 儘管它仍然是註冊的。一旦你再次連接你的意圖應該仍然是註冊。

+0

請參閱澄清的問題 – cja

+0

請參閱我的編輯。 –

+0

+1這麼寫 – cja

相關問題