Saving battery power on Android: Third-party apps and wake locks

Edit July 3, 2014:

Originally written based on Android 4.0, this post is now over two years old. I now own a Nexus 5 running Android 4.4.4, and the battery monitoring screen has apparently improved a great deal since version 4.0:
If any app holds a wake lock for an excessive amount of time, it will show up clearly as a power hog on the Settings –> Battery screen. This should make battery life troubleshooting much easier than it was in the past. Kudos to Google for this improvement.

 

Here is the original post from Feb 16, 2012:

Introduction
If you have an Android device and enjoy installing and using many different apps, chances are that you may have run into the problem of rapid battery drain even while the screen is off.

If you Google “how to save battery on Android”, you will often find tips such as disabling various wireless radios, lowering your screen timeout, lowering your brightness, etc. These tips may help, but a potentially more serious culprit is some third-party app constantly keeping the system awake, draining power in the background. If this is the case on your phone, it will by far outweigh the consumption of your wireless radios, possibly reducing your battery life to as little as 8 hours.
Continue reading

Posting source code on a WordPress.com blog

Perhaps you – like I was – are trying to post source code on your wordpress.com hosted blog but can’t get it to display properly. You’ve inserted the ‘sourcecode’ tag in HTML view, but when you hit the ‘Visual’ tab of the editor, it still comes out like this:

Perhaps you’ve already followed the instructions on this page to no avail.

As it turns out, the Visual preview feature of the editor simply doesn’t fully render your post. You need to use the ‘Preview’ button to determine whether your sourcecode tags are working:

public void WriteToConsole()
{
Debug.WriteLine("This is what you should see when you hit 'Preview'!");
}

And that’s really all there is to it. You don’t need to add a plugin or anything – it’s already available for you to use.

Obtaining the latest cached location in a Background Agent

Today, in a Windows Phone 7.5 “Mango” app, I needed to retrieve the phone’s most recent location and send it to a server from a background agent.

On this MSDN page, Microsoft suggests doing this by using the GeoCoordinateWatcher API:

This API, used for obtaining the geographic coordinates of the device, is supported for use in background agents, but it uses a cached location value instead of real-time data. The cached location value is updated by the device every 15 minutes.

Excellent. So how do we actually go about retrieving the latest cached location? I couldn’t find a code example of this anywhere. It would probably make sense for the API to have a static, no-hassle method that returns the cached location, but it doesn’t provide that.

The following code will return the latest available location (up to 15 minutes old):

private GeoPosition GetCachedLocation()
{
    GeoCoordinateWatcher geoWatcher;
    //Start a new watcher with default level of accuracy
    geoWatcher = new GeoCoordinateWatcher();
    geoWatcher.Start();
    //Get latest cached position
    GeoPosition position = geoWatcher.Position;
    geoWatcher.Stop();
    return position;
}

Note that this method will execute even if you leave out geoWatcher.Start(); and geoWatcher.Stop();, which is what I tried at first, but then your location will contain empty values.

This satisfies my requirement. If you need some control over the provided accuracy, you could try creating your GeoCoordinateWatcher with a self-defined accuracy requirement using the GeoCoordinateWatcher(GeoPositionAccuracy) constructor, but I haven’t tested this. Since you can’t request a fresh location, perhaps it would simply return the latest available cached location that satisfies the given accuracy requirement?