[This post is by , Tech Lead for Android Developer Relations, who on Android App development. — Tim Bray]
http://ads.buzzcity.net/adpage.php?partnerid=40096
分类: 嵌入式
2011-06-24 09:36:03
[This post is by , Tech Lead for Android Developer Relations, who on Android App development. — Tim Bray]
I'm a big fan of location-based apps, but not their seemingly-inevitable startup latency.
Whether it's finding a place to eat or searching for the nearest , I find the delay while waiting for the GPS to get a fix, and then for the results list to populate, to be interminable. Once I’m in a venue and ready to get some tips, check-in, or review the food, I’m frequently thwarted by a lack of data connection.
Rather than shaking my fist at the sky, I’ve written an open-source reference app that incorporates all of the tips, tricks, and cheats I know to reduce the time between opening an app and seeing an up-to-date list of nearby venues - as well as providing a reasonable level of offline support — all while keeping the impact on battery life to a minimum.
Show Me the CodeYou can check-out the open source project from Google Code. Don’t forget to read the for the steps required to make it compile and run successfully.
What Does it Actually Do?It uses the Google Places API to implement the core functionality of apps that use location to provide a list of nearby points of interest, drill down into their details, and then check-in/rate/review them.
The code implements many of the best-practices I detailed in my Google I/O 2011 session,Android Protips: Advanced Topics for Expert Android Developers (), including using Intents to receive location updates, using the Passive Location Provider, using and monitoring device state to vary refresh rates, toggling your manifest Receivers at runtime, and using the Cursor Loader.
The app targets Honeycomb but supports Android platforms from 1.6 and up.
Nothing would make me happier than for you to cut/copy/borrow / steal this code to build better location-based apps. If you do, I’d love it if you !
Now that you’ve got the code, let’s take a closer look at itMy top priority was freshness: Minimize the latency between opening the app and being able to check in to a desired location, while still minimizing the impact of the app on battery life.
Related requirements:
The current location has to be found as quickly as possible.
The list of venues should update when the location changes.
The list of nearby locations and their details must be available when we’re offline.
Check-ins must be possible while we’re offline.
Location data and other user data must be handled properly (see our prior blog post on best practices).
You can significantly reduce the latency for getting your first location fix by retrieving the last known location from the Location Manager each time the app is resumed.
In this snippet taken from the , we iterate through each location provider on the device — including those that aren't currently available — to find the most timely and accurate last known location.
List<String> matchingProviders = locationManager.getAllProviders();If there is one or more locations available from within the allowed latency, we return the most accurate one. If not, we simply return the most recent result.
In the latter case (where it’s determined that the last location update isn't recent enough) this newest result is still returned, but we also request a single location update using that fastest location provider available.
if (locationListener != null &&Unfortunately we can’t specify “fastest” when using Criteria to choose a location provider, but in practice we know that coarser providers — particularly the network location provider — tend to return results faster than the more accurate options. In this case I’ve requested coarse accuracy and low power in order to select the Network Provider when it’s available.
Note also that this code snippet shows the which uses the requestSingleUpdate method to receive a one-shot location update. This wasn’t available prior to Gingerbread - check out the to see how I have implemented the same functionality for devices running earlier platform versions.
The singleUpdateReceiver passes the received update back to the calling class through a registered Location Listener.
protected BroadcastReceiver singleUpdateReceiver = new BroadcastReceiver() {Having obtained the most accurate/timely estimate of our current location, we also want to receive location updates.
The class includes a number of values that determine the frequency of location updates (and the associated server polling). Tweak them to ensure that updates occur exactly as often as required.
// The default search radius when searching for places nearby.The next step is to request the location updates from the Location Manager. In this snippet taken from the we can pass the Criteria used to determine which Location Provider to request updates from directly into the requestLocationUpdates call.
public void requestLocationUpdates(long minTime, long minDistance,Note that we're passing in a Pending Intent rather than a Location Listener.
Intent activeIntent = new Intent(this, LocationChangedReceiver.class);I generally prefer this over using Location Listeners as it offers the flexibility of registering receivers in multiple Activities or Services, or directly in the manifest.
In this app, a new location means an updated list of nearby venues. This happens via a Service that makes a server query and updates the Content Provider that populates the place list.
Because the location change isn’t directly updating the UI, it makes sense to create and register the associated in the manifest rather than the main Activity.
The Location Changed Receiver extracts the location from each update and starts the to refresh the database of nearby locations.
if (intent.ha***tra(locationKey)) {The snippet from below shows how to monitor two important conditions:
The Location Provider we are using being deactivated.
A better Location Provider becoming available.
In either case, we simply re-run the process used to determine the best available provider and request location updates.
// Register a receiver that listens for when the provider I'm using has been disabled.You can start the in the background to refresh the list of nearby locations while your app is in the background. Done correctly, a relevant list of venues can be immediately available when you open the app.
Done poorly, your users will never find this out as you’ll have drained their battery too quickly.
Requesting location updates (particularly using the GPS) while your app isn’t in the foreground is poor practice, as it can significantly impact battery life. Instead, you can use the Passive Location Provider to receive location updates alongside other apps that have already requested them.
This extract from the enables passive updates on Froyo+ platforms.
public void requestPassiveLocationUpdates(long minTime, long minDistance, PendingIntent pendingIntent) {As a result receiving background location updates is effectively free! Unfortunately the battery cost of your server downloads aren’t, so you’ll still need to carefully balance how often you act on passive location updates with battery life.
You can achieve a similar effect in pre-Froyo devices using inexact repeating non-wake alarms as shown in the .
public void requestPassiveLocationUpdates(long minTime, long minDistance,Rather than receiving updates from the Location Manager, this technique manually checks the last known location at a frequency determined by the maximum location update latency.
This legacy technique is significantly less efficient, so you may choose to simply disable background updates on pre-Froyo devices.
We handle updates themselves within the which determines the current location and starts the .
if (location != null) {You’ll note that we registered the Passive Location Changed Receiver in the application manifest.
As a result we can continue to receive these background updates even when the application has been killed by the system to free resources.
This offers the significant advantage of allowing the system to reclaim the resources used by your app, while still retaining the advantages of a zero latency startup.
If your app recognizes the concept of “exiting” (typically when the user clicks the back button on your home screen), it’s good form to turn off passive location updates - including disabling your passive manifest Receiver.
Being fresh means working offlineTo add offline support we start by caching all our lookup results to the and .
Under certain circumstances we will also pre-fetch location details. This snippet from the shows how pre-fetching is enabled for a limited number of locations.
Note that pre-fetching is also potentially disabled while on mobile data networks or when the battery is low.
if ((prefetchCount < PlacesConstants.PREFETCH_LIMIT) &&We use a similar technique to provide support for offline checkins. The queues failed checkins, and checkins attempted while offline, to be retried (in order) when the determines that we’re back online.
Optimizing battery life: Smart Services and using device state to toggle your manifest ReceiversThere's no point running update services when we aren’t online, so the checks for connectivity before attempting an update.
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();If we’re not connected, the Passive and Active Location Changed Receivers are disabled and the the is turned on.
ComponentName connectivityReceiver =The listens for connectivity changes. When a new connection is made, it simply disables itself and re-enables the location listeners.
Monitoring battery state to reduce functionality and save powerWhen your phone is on its last 15%, most apps are firmly in the back seat to conserving what watts you have remaining. We can register manifest Receivers to be alerted when the device enters or leaves the low battery state.
This snippet from the disables the whenever the device enters a low battery state, and turns it back on once the battery level is okay.
boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW);You can extend this logic to disable all prefetching or reduce the frequency of your updates during low battery conditions.
What’s Next?This is already a monster post, so I’m going to leave it there. I’ll follow up in the next week with a post on my personal blog, The Radioactive Yak, that will go in to more detail on the psychic and smooth elements of this app like using the Backup Manager and the Cursor Loader.
I also plan to build a similar reference app for news apps, so that I can spend more time reading and less time waiting.
In the mean time, happy coding!