battery efficient location services
DESCRIPTION
DroidCon - NYC 2014 PresentationTRANSCRIPT
![Page 1: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/1.jpg)
Battery Efficient Location Services
NYC Droidcon, 9/21/14google.com/+ArunNagarajan @entaq
![Page 2: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/2.jpg)
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
![Page 3: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/3.jpg)
Background
![Page 4: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/4.jpg)
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
![Page 5: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/5.jpg)
Three variables to consider
● Power● Accuracy● Coverage
![Page 6: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/6.jpg)
Various options
Source - developers.google.com/events/io/sessions/325337477
![Page 7: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/7.jpg)
Battery cost is real
Source - developers.google.com/events/io/sessions/325337477
![Page 8: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/8.jpg)
What takes up more power?
● CPU● Display● GPS● WiFi
What’s your guess?
![Page 9: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/9.jpg)
What takes up more power?
● CPU ~ 300 milliwatts● Display ~ 350 milliwatts● GPS ~100 milliwatts● WiFI ~30 milliwatts
![Page 10: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/10.jpg)
User first
![Page 11: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/11.jpg)
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
![Page 12: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/12.jpg)
Tools the user has
● Battery UI● GPS indicator
○ Some Samsung devices blink indicator!
![Page 13: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/13.jpg)
But there is more
![Page 14: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/14.jpg)
Critical APIs
![Page 15: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/15.jpg)
Key Resources
d.android.com/training/location/index.html
d.android.com/guide/topics/location/index.htmld.android.com/guide/topics/sensors/index.html
![Page 16: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/16.jpg)
Simple/Synchronous waypublic class MainActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
...
// Global variable to hold the current location
Location mCurrentLocation;
...
mCurrentLocation = mLocationClient.getLastLocation();
...
}
![Page 17: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/17.jpg)
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);
![Page 18: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/18.jpg)
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());
}
...
}
![Page 19: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/19.jpg)
Register a pending intentPendingIntent bglocationUpdate = PendingIntent.getService(context, 0,
new Intent(context, BackgroundLocationUpdateService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
locationClient.requestLocationUpdates(locationRequest,bglocationUpdate);
locationClient.disconnect();
![Page 20: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/20.jpg)
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);
}
![Page 21: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/21.jpg)
Other tricks
![Page 22: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/22.jpg)
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";
![Page 23: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/23.jpg)
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>
![Page 24: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/24.jpg)
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();
}
}
![Page 25: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/25.jpg)
Upcoming
![Page 26: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/26.jpg)
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
![Page 27: Battery Efficient Location Services](https://reader033.vdocuments.mx/reader033/viewer/2022052507/55819844d8b42a417f8b50c4/html5/thumbnails/27.jpg)
Future explorations
● BLE devices● Other nearby phones● Photo uploads● Android L - Job Scheduler