开发者

GPS Service in Android

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.

开发者_Python百科

Thanks a lot for your help, and sharing your knowledge with me.

BR. David.


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...

  • Never assume there are location providers. The device may have no network connection, or no GPS. You need error handling in the code to handle those scenarios.
  • Always use threads to do geo-lookup to keep the app responsive
  • Try to use the Android last-known location if possible, it provides a quick lock. You could update the location in the background to a more accurate one.
  • If the app may run on devices that cannot do geo-location (e.g. cheaper tablets, Google TV etc) store the location in preferences, so the user specific it once, and it uses that each time the app loads (if it cannot find a new location)
  • It often makes sense to create a class specifically to manage getting locations, threading, etc. Then other classes within your app can simply call a static method just like you suggest, but the challenge is the code you use to get a value into your variable mLocation!
  • You may not get a location at all - don't assume you will, devices & contexts vary.
  • Unless you MUST have a very accurate location, always use the network location providers too as they are often very quick. A GPS location lookup from fresh can be very slow, and users will get bored and frustrated. Google Maps seems quite quick, but it actually does quite a bit of location caching, and you'll see sometimes it is slow to get a fresh location.

I recommend Mark Murphy's e-books on Android, he has excellent coverage and source code examples for geolocation http://commonsware.com/


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜