在使用 chromecast 发件人应用程序时,我很难找到用于在拆卸实现中取消注册 com.google.android.gms.cast.Cast.Listener 的 api。
这就是构建 api 客户端的方式:
CastListener castListener = new CastListener();
Cast.CastOptions.Builder apiOptionsBuilder = Cast.CastOptions
.builder(device, castListener);
GoogleApiClient apiClient = new GoogleApiClient.Builder(context)
.addApi(Cast.API, apiOptionsBuilder.build())
.addConnectionCallbacks(connectionCallbacks)
.addOnConnectionFailedListener(connectionFailedListener)
.build();
这里是我的 teardown() 实现:
apiClient.unregisterConnectionCallbacks(connectionCallbacks);
apiClient.unregisterConnectionFailedListener(connectionFailedListener);
apiClient.disconnect();
apiClient = null;
似乎有用于连接回调和失败侦听器的 unregister* 方法,但没有用于删除强制转换侦听器。因此,即使在拆卸之后,CastListener 方法仍在执行(onVolumeChanged),所以我认为 GoogleApiClient 的处置尚未完成。在拆解中还有什么可以做的吗?类似于 GoogleApiClient 上的隐藏 removeApi() 方法?
目前在我的 CastListener.onVolumeChanged() 中有 apiClient!=null 检查,但我想避免这种情况,而是有一个解决方案,将演员监听器完全置于 teardown() 中。
有任何想法吗?