gpt4 book ai didi

android - 检查 Android 应用程序 LocationClient 是否正在请求位置更新

转载 作者:行者123 更新时间:2023-11-30 02:59:16 25 4
gpt4 key购买 nike

我正在使用 PendingIntent 调用 requestLocationUpdates。稍后我想知道这个请求是否仍然有效。我该怎么做?

我知道我是否调用了 removeLocationUpdates,但我希望可以通过其他方式停止位置更新,我不想出错。

这个问题与地理围栏无关。

最佳答案

根据:

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.

所有这些都会告诉您有关特定听众的信息。我没有看到任何代码可以一般性地回答“你有没有人在听你讲任何事情”这个问题?

您可能会使用内省(introspection)来强制 API 使其某些内部结构可见,但您可能可以通过其他不那么丑陋的方式来完成您想要的事情。

你想完成什么?

编辑:

同样来自文档:

public void requestLocationUpdates (LocationRequest request, PendingIntent callbackIntent)

Requests location updates with a callback on the specified PendingIntent.

This method is suited for the background use cases, more specifically for receiving location updates, even when the app has been killed by the system. In order to do so, use a PendingIntent for a started service. For foreground use cases, the LocationListener version of the method is recommended, see requestLocationUpdates(LocationRequest, LocationListener).

Any previous LocationRequests registered on this PendingIntent will be replaced.

这是什么意思?

1) 您的请求已暂存,并且不会取消暂存,直到您明确调用

public void removeLocationUpdates (PendingIntent callbackIntent)

2) 即使您的应用已被系统终止,也是如此。 (假设后台用例。我不确定前台用例)。

3) 如果 LocationClient 已停止接收更新 - 您的 Intent 将不会收到通知。您必须调用:

isConnected() Checks if the client is currently connected to the service, so that requests to other methods will succeed. boolean
isConnecting() Checks if the client is attempting to connect to the service.

Location updates are sent with a key of KEY_LOCATION_CHANGED and a Location value on the intent.

Parameters request The location request for the updates. callbackIntent A pending intent to be sent for each location update.

所以后面一点 - 我会检查 LocationClient 是否已连接/正在连接。如果不是,可以安全地假设您的 Intent 不会被触发——尽管它仍然是注册的。再次连接后,您的 Intent 应该仍然被注册。

关于android - 检查 Android 应用程序 LocationClient 是否正在请求位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22793535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com