Reputation: 2003
I have just started playing with the location services that android provides, I was thinking about the following code:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//update textview
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
When the location updates I was going to update a text view with the speed using the getSpeed()
method that is available.
My question is, does this nuke the battery life of the phone? if so, are there more efficient ways of doing it? like registering a BroadcastReceiver
to get the location updates? I'm just looking for ways that I can use the Location updates in a way that is efficient for maximising battery life.
Upvotes: 0
Views: 201
Reputation: 8134
The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.
Source: Making Your App Location-Aware
I have used it and found the following to be quite efficient, esp in terms of nuking the battery vs obtaining the location frequently, accurately:
1. Use Fused Location Provider, GoogleApiClient:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
2. Use LocationRequest according to the requirement:
mLocationRequest = new LocationRequest();
mLocationRequest.setFastestInterval(120000);
//there are other options associated with this objects
// including types of priority
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
3. Set the LocationServices to use a this
if class implements LocationListener
or define a new one in:
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
All components are under the package
com.google.android.gms
Reference: googlesamples/android-play-location
Upvotes: 3