Reputation: 4003
I have a question about services in Android. I want to know what's the best approach to get the Location variables from GPS. I am doing an application with geolocation using a service wich implements the GPS module. And for getting the location I create a variable in the class static and public method static to be called from outside the class, and that is it. To retrieve the position from the GPS I just used in my classes:
Location mLocation = ServiceGPS.getLocation(); The class ServiceGPS has this method:
public static Location getLocation(){ return mLocation; }
I dont really see the point of binding to a service in this case, as I just have to call the static method. I also was thinking of doing a base class or implements the LocationListener, but this way is really simple and it works and saves code writting.
Thanks a lot for your help, and sharing your knowledge with me.
BR. David.
Upvotes: 3
Views: 8357
Reputation: 4003
I am not just getting the position... Also I am cheking the provider calling another method of the service.And depending of that I will or not throw an exception.The proccesing that I do afterward is not the issue.I am grateful to you Ollie for the information. But my question is:
I dont really see the point of binding to a service in this case, as I just have to call the static method. I also was thinking of doing a base class or implements the LocationListener, but this way is really simple and it works and saves code writting.
And actually I am running my Location in a Service, and therefore in another thread, so I dont get your point. It seems the question would be, How does Location in Android works? Cheers David
Upvotes: 0
Reputation: 28509
You can't just "get" the location in Android, as it may come from different places (the last-known location, the GPS receiver, the wifi network location, or the mobile data network) and more importantly it may take time to receive the location or you may not get one at all (e.g. GPS present, but no signal as indoors).
The fact that it may take time to get the location means you MUST run the location-seek on a separate thread, unless there is a previously cached location. Otherwise your application will lock up until the location is acquired (which may never happen, e.g. on a device with no GPS or geo-locating network).
This is the key page you need to read, digest, and use: http://developer.android.com/guide/topics/location/obtaining-user-location.html If you're new to geo-location in Android, use the example code to start with.
The things I've learnt adding geo-location to apps are...
I recommend Mark Murphy's e-books on Android, he has excellent coverage and source code examples for geolocation http://commonsware.com/
Upvotes: 9