如果您连接和断开连接到谷歌播放服务中的每一项活动? [英] Should you connect and disconnect to Google Play Services in each activity?

查看:172
本文介绍了如果您连接和断开连接到谷歌播放服务中的每一项活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这需要一个连接的位置客户端中的所有活动的应用程序。如何在管理客户端的状态?

I am writing an application which needs a connected location client in all the activities. How do the manage the state of the client?

我要叫 mLocationClient.connect()一次,以避免麻烦,而且应该能够删除位置更新/断开时,应用程序停止。

I want to call the mLocationClient.connect() only once to avoid hassle, and should be able to remove location updates / disconnect when the application stops.

如何保持所有活动的连接位置客户端,假设我在闪屏连接到它的活动

How do I keep the location client connected across all activities, assuming I have connected to it in the splash screen Activity?

另外一个疑问,就出现在这里,当我恢复暂停的应用程序(而不是娱乐),应用程序将无法启动的启动画面。如何维护在此情况下,连接

Another question that arises here is, when I resume the paused application (not recreation), the app won't start with the splash screen. How do I maintain the connection in this case?

在此先感谢。

推荐答案

你需要的是一个的绑定服务:这些服务只能住而客户端(如您的活动之一)连接到它。这可以让你有一个共享的状态(单个连接的位置客户端),同时还确保您连接/断开适当。

What you need is a bound service: these services only live while a client (like one of your activities) is connected to it. This allows you to have a shared state (a single connected location client) while still ensuring that you connect/disconnect appropriately.

在这种情况下,任何位置感知活动会结合到服务。当第一个活动(例如,闪屏活动)绑定到该服务,该服务将启动并连接到谷歌播放服务。您的服务的文件夹将随后向任何有关的活动获得了 LocationClient 。当你活动之间移动,每次将绑定到转服务,并能够获得当前位置数据和每个会,因为他们被摧毁。

In this situation, any location aware activity would bind to the service. When the first activity (say, your splash screen activity) binds to the service, the service would start and connect to Google Play Services. Your service's Binder would then give access to its LocationClient to any connected activities. As you move between activities, each would bind to the service in turn and be able to get the current location data and each would as they get destroyed.

当用户退出应用程序(即最后一项活动被破坏),那么该服务将自动停止本身,让您从谷歌断开播放服务。

When the user exits your application (i.e., the last activity is destroyed), then the service would automatically stop itself, allowing you to disconnect from Google Play Services.

只要您绑定从每一个需要的位置数据的活动服务,它并不重要的活动开始最初的服务:如果需要服务将只需连接

As long as you bind to the service from every activity that needs location data, it doesn't matter which activity starts the service initially: the service would just connect if needed.

这篇关于如果您连接和断开连接到谷歌播放服务中的每一项活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆