Use one LocationClient for Current Location and Geofence or use two different LocationClient

906 views Asked by At

I have created one class called LocationHelper to get the current location of the phone. With that info I show a list of Shops and the distance to my current position. I used one LocationClient and LocationRequest, on the event onLocationChanged I sent a BroadcastIntent and in my activity, I do what I need. It works ok.

Now, for other funcionality of my app, I need work with Geofence. The idea is show a Notification when the cellphone user is near to a shop My first approach was write another LocationHelper called LocationHelperGeofence. Using the oficial tutorial, I made the class. It works ok

Both classes works ok, but I have 2 LocationClient in each class. Also in each class I check if Google Play Services is Available and have another code duplicated.

My questions are:

It is this approach right? It is the best way to work have 2 Classes with 2 LocationClients? Or it is better have only one class for getting the current location and use geofences?

Thanks and sorry if my english it isn't the best

1

There are 1 answers

1
fasteque On

As per my experience, you cannot have 2 LocationClient instances in the same class, basically because, for example, how can you override onConnected method for both? In java you cannot do that.

So my suggestion is to have the connection to those to client managed in separate classes.

Download the example here: http://developer.android.com/training/location/activity-recognition.html

You'll find what you're looking for.