Battery Efficient Location Services
-
Upload
arun-nagarajan -
Category
Technology
-
view
378 -
download
4
description
Transcript of Battery Efficient Location Services
Battery Efficient Location Services
NYC Droidcon, 9/21/14google.com/+ArunNagarajan @entaq
About me
Arun Nagarajan (@entaq)
Currently Founding Lead Engineer, funded stealth startup (NYC)We are hiring! Email me at [email protected]
Previously2 yrs at Google - Tech Lead, Developer Platform9 yrs at Verivo Software (Boston) - VP of Architecture
Background
What are location services?
● Maps vs. Location○ Context is king
● Capabilities over technologies● User expectations
○ Privacy, transparency, no compromises● Background location
○ Intelligent services that become possible
Three variables to consider
● Power● Accuracy● Coverage
Various options
Source - developers.google.com/events/io/sessions/325337477
Battery cost is real
Source - developers.google.com/events/io/sessions/325337477
What takes up more power?
● CPU● Display● GPS● WiFi
What’s your guess?
What takes up more power?
● CPU ~ 300 milliwatts● Display ~ 350 milliwatts● GPS ~100 milliwatts● WiFI ~30 milliwatts
User first
Transparency to the user
● Having a map (a reason) in your app helps● Permissions
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
● Showing in the UI
Tools the user has
● Battery UI● GPS indicator
○ Some Samsung devices blink indicator!
But there is more
Critical APIs
Key Resources
d.android.com/training/location/index.html
d.android.com/guide/topics/location/index.htmld.android.com/guide/topics/sensors/index.html
Simple/Synchronous waypublic class MainActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
...
// Global variable to hold the current location
Location mCurrentLocation;
...
mCurrentLocation = mLocationClient.getLastLocation();
...
}
Configure a LocationRequestmLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setSmallestDisplacement(DISTANCE_IN_METERS);
mLocationClient.requestLocationUpdates(mLocationRequest, this);
Callback to LocationListenerpublic class MainActivity extends FragmentActivity implements LocationListener { ...
@Override
public void onLocationChanged(Location location) {
String msg = "Updated Location: " + Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
}
...
}
Register a pending intentPendingIntent bglocationUpdate = PendingIntent.getService(context, 0,
new Intent(context, BackgroundLocationUpdateService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
locationClient.requestLocationUpdates(locationRequest,bglocationUpdate);
locationClient.disconnect();
AlarmManager works best for usPendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this,
AlarmManagerLocationReceiver.class), 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (foreground) {
alarmMgr.cancel(pi);
} else {
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 1000 * 60 * 10, pi);
}
Other tricks
Activity Monitoring switch(activityType) {
case DetectedActivity.IN_VEHICLE:
return "in_vehicle";
case DetectedActivity.ON_BICYCLE:
return "on_bicycle";
case DetectedActivity.ON_FOOT:
return "on_foot";
case DetectedActivity.STILL:
return "still";
case DetectedActivity.UNKNOWN:
return "unknown";
case DetectedActivity.TILTING:
return "tilting";
}
return "unknown";
Watch the battery life in your code
boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW);
<receiver android:name=".receivers.PowerStateChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
<action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
</intent-filter>
</receiver>
GeofencemLocationClient.addGeofences(mCurrentGeofences, pendingIntent, this);
if ((transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) ||
(transitionType == Geofence.GEOFENCE_TRANSITION_EXIT)
) {
List <Geofence> triggerList =
getTriggeringGeofences(intent);
String[] triggerIds = new String[geofenceList.size()];
for (int i = 0; i < triggerIds.length; i++) {
triggerIds[i] = triggerList.get(i).getRequestId();
}
}
Upcoming
Battery Historian
Project Volta$ adb shell dumpsys batterystats --charged <package-name>
$ adb shell dumpsys batterystats --enable full-wake-history
$ adb shell dumpsys batterystats --reset
$ historian.par [-p powerfile] bugreport.txt > out.html
github.com/google/battery-historian
Future explorations
● BLE devices● Other nearby phones● Photo uploads● Android L - Job Scheduler