On Fri, Feb 18, 2011 at 12:27 PM, Jake Colman <[email protected]> wrote: > Lets say you have a widget that displays the number of minutes remaining > until Sunrise. The widget itself cannot, in its own context, determine > its location for astronomical calculations since that takes too long. > So we use a service to do the actual work of figuring out where we are > and what time is Sunrise. The widget sets up an alarm so that its > broadcast receiver gets a notification every 60 seconds (or configurable > time period) so as to start the service and update the widget. > > If I understand Mark's books and other sources correctly this should be > the correct way to approach the problem - I hope!
The notion of using a service to figure out where we are and when sunrise is seems reasonable. I am not particularly a fan of updating an app widget every 60 seconds, though the configurable time period is reasonable. > So how do I now handle the location issue? getLastKnownLocation() won't > work, as pointed out, since there may not be one. So do I have the > service set up a location listener to et the initial update and then > only take new updates when the location changes by X miles? Or is the > service not the correct place to have the listener? This is a complex problem. I took a stab at it with my LocationPoller: https://github.com/commonsguy/cwac-locpoll Basically, it is designed to be woken up by an AlarmManager alarm and then figure out where you are, taking into account WakeLocks, the delay in getting a location fix, and so on. I tested it with GPS, though it should work with the network provider as well. So, you would use an alarm with LocationPoller to figure out where you are periodically, and tag along the logic to then compute sunrise. Allow the user to configure how frequently you check your location, as well as how frequently to update the app widget display (hint: checking for locations every 60 seconds will *so* not be a good idea). Note that LocationPoller is rather experimental code at this stage. Your kilometerage may vary. I thought that LocationPoller would not be needed with the PendingIntent-based requestLocationUpdates(), but I had poor results with that. > At every service start the service needs to know where it is so that it > can update the time to sunrise in case we moved. So maybe what I really > need is a one-shot quick location fix for the current location and not a > listener. I don't care to know where I am unless I am ready to update > the widget. You might not even be able to get a fix in 60 seconds (e.g., user is in a spot with no signal). -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy Android 2.3 Programming Books: http://commonsware.com/books -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

