« Moving a SharePoint site collection to another managed path | Main | Creating a static WSDL through reflection »
Saturday
Feb062010

Battery Conscious: Using your GPS with Android

With that Android OS it is straight forward to use your GPS to get your location. Starting out you may get your location manager and register for updates so your program get’s notified of changes in position. It may look something like this:

LocationManager locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new LocationListener()
{
    public void onLocationChanged(Location location)
    {
        if (location != null)
        {
            Toast.makeText(getBaseContext(),
            "Position: latitude [" +
            location.getLatitude() +
            "] longitude [" + location.getLongitude()+"]",
            Toast.LENGTH_SHORT).show();
        }
    }
 
    public void onProviderDisabled(String provider)
    {
    }
 
    public void onProviderEnabled(String provider)
    {
    }
 
    public void onStatusChanged(String provider,
    int status, Bundle extras)
    {
    }
 
};
 
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locListener);


This is a typical example you might see floating around on the internet or in an Android Pro book. The line that requests location updates takes the following parameters

 

1. GPS Provider

2. Minimum time between updates in milliseconds

3. Minimum distance between updates in meters

4. Location Listener

 

The above implementation will actually keep you GPS active all the time and drain your battery in less than 5 hours. It also doesn’t take into account your actual needs when specifying the location provider, in this case the GPS. Let’s take a look at how to improve this code to save battery life.

The first thing you can do is set up criteria based on your needs to get the best GPS provider available. For instance, if you only need to get your location (lat, lon) without any information on bearing or speed, and require that the battery have at least a medium charge to use the GPS then you can do so. The following example uses Criteria to get the best GPS provider and limits the time and distance between updates to save battery life.

private void SetupGPSListener()
{
    Criteria criteria = new Criteria();
    
    criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    
    criteria.setAltitudeRequired(false);
    criteria.setSpeedRequired(false);
    criteria.setBearingRequired(false);
    
    // Indicates whether the provider is allowed to incur monetary cost.
    criteria.setCostAllowed(true);
    
    // Most location sensitive apps will need to be reactive to user movement.
    // Simply polling the Location Manager will not force it to get new updates from the Location Providers.
    // Use the requestLocationUpdates method to get updates whenever the current location changes
    
    // the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, 
    // and actual time between location updates may be greater or lesser than this value.
    int minTimeBetweenUpdatesms = 1000 * 60;
    
    // the minimum distance interval for notifications, in meters
    int minDistanceBetweenUpdatesMeters = 20; // 100 ft
    
    // Enable location providers
    // This method enables each provider and returns the last known location.
    // Also requests periodic updates for each provider to force android to start updating the locations for the other applications.
    String provider = locationManager.getBestProvider(criteria, true);
            
    locationManager.requestLocationUpdates(provider, minTimeBetweenUpdatesms, minDistanceBetweenUpdatesMeters, new LocationListener() {
        
        public void onLocationChanged(Location location) {
            currentLatitude = location.getLatitude();
            currentLongitude = location.getLongitude();                    
        }
 
        public void onProviderDisabled(String provider) {
            // Update application if provider disabled.
            Log.i(LOG_TAG, "GPS Provider is disabled");
        }
 
        public void onProviderEnabled(String provider) {
            // Update application if provider enabled.
            Log.i(LOG_TAG, "GPS Provider is enabled");
        }
 
        public void onStatusChanged(String provider, int status, Bundle extras) {
            
        }                
    });    
}

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>