java - 在 `removeLocationUpdates` 的 `FusedLocationClientProvider` 完成时接收回调的最佳方式是什么

标签 java android google-maps-android-api-2 fusedlocationproviderapi

我有以下代码按预期工作但是当我请求使用下面显示的 stopLocationUpdates 方法删除位置更新时,我的 onCompleteListener 附加到 removeLocationUpdates 不会在第一次请求时触发,尽管位置更新已按要求停止。但是,在下一次请求时,将触发 onCompleteListener 回调。

/**
 * Provides access to the fused location provider API
 */
private FusedLocationProviderClient mFusedLocationClient;


/**
 * Removes location updates from the FusedLocationApi
 */
private void stopLocationUpdates() {
    if (!mRequestingLocationUpdates) {
        // Updates were never requested.
        return;
    }

    // Remove location request when activity is in a paused or stopped state.
    mFusedLocationClient.removeLocationUpdates(mLocationCallback)
            .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    mRequestingLocationUpdates = false;
                }
            });
}

最佳答案

我没有 50 次回复。评论,所以我在这里写下我的评论。

如果你添加一个.addOnFailureListener,它会在第二次拍摄时报告这样的错误吗?

W/System.err: com.google.android.gms.common.api.ApiException: 13: listener already unregistered

RG

2017.04.17 更新:

添加了 .addOnFailureListener(getActivity(), new OnFailureListener()...) 和 .addOnSuccessListener(getActivity(), new OnFailureListener()...) 并发现以下内容:

  1. 三个监听器(包括初始的 addOnCompleteListener(getActivity(), new OnCompleteListener()...))都没有在此(第一个)removeLocationUpdate 请求上调用。

  2. 在第二个请求中,首先调用 addOnCompleteListener 监听器(一切似乎都正常),然后调用 onFailureListener 监听器并出现以下错误:

    W/System.err: com.google.android.gms.common.api.ApiException: 13: listener already unregistered

似乎监听器直到第二个请求才得到第一个回调(它是否堆积在某个堆栈上?),并且它调用(正确?)错误监听器以告知监听器已经取消注册...

很奇怪……不奇怪吗?

无论如何我都会提交一个错误报告,如果它还没有完成的话。

我使用 v11.4.2 firebase、v11.4.2 play-services、v26.1.0 android.support 和 v3.1.1 of com.google.gms:google-services。

RG

关于java - 在 `removeLocationUpdates` 的 `FusedLocationClientProvider` 完成时接收回调的最佳方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46401266/

相关文章:

android - Robolectric 3 的图书馆资源 - JodaTime

android - 使用谷歌位置 API 在 android 中的 onMapReady 中获取当前位置

android - 第二次单击标记时隐藏 infoWindow(Google Map Android API V2)

java - 如何格式化具有短名称 ID 和长名称的时区?

java - Itext 5.5 + Hwcrypto.js 添加签名 "Document has been altered or corrupted since it was signed"

java - 创建标题页 Activity

android - react native : Stomp websocket not working in android

android - Espresso onView 不等待元素

android - 无法将 google-play-services 正确安装到 Eclipse 中(试图让 map 正常工作)

java - JMH 多个基准 单独运行的不同结果