Should you connect to and disconnect from Google Play services at every event?

I am writing an application that needs a connected location client in all actions. How to manage client status?

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

How do I bind a location client to all actions, assuming I connected to it in the Activity splash screen?

Another question that arises here when I resume a paused application (rather than recreation) is that the application will not start with a splash screen. How can I support the connection in this case?

Thanks in advance.

+4
source share
1 answer

What you need is a related service : these services only work if a client is connected to it (for example, one of your actions). This allows you to have a common state (one connected location client), while still ensuring that you connect / disconnect appropriately.

In this situation, any activity associated with the location will be bound to the service. When the first activity (for example, your splash screen activity) is associated with a service, the service will start working and connect to Google Play services. Your Binder service will then provide access to its LocationClient for any related activities. When you move between actions, each of them will be attached to the service in turn and will be able to receive current location data, and each of them will be destroyed.

When a user exits your application (i.e. the last action will be destroyed), the service will automatically stop, which will allow you to disconnect from Google Play services.

As long as you get attached to the service from every action that needs location data, it does not matter which of the service starts initially: the service will simply connect if necessary.

+4
source

Source: https://habr.com/ru/post/970640/


All Articles