Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Battery Efficient 
Location Services 
google.com/+ArunNagarajan @entaq 
NYC Droidcon, 9/21/14
About me 
Arun Nagarajan (@entaq) 
Currently 
Founding Lead Engineer, funded stealth startup (NYC) 
We are hiring! Email m...
Background
What are location services? 
● Maps vs. Location 
○ Context is king 
● Capabilities over technologies 
● User expectations...
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 milliwatt...
User first
Transparency to the user 
● Having a map (a reason) in your app helps 
● Permissions 
<uses-permission android:name="andro...
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.html 
d.android.com/g...
Simple/Synchronous way 
public class MainActivity extends FragmentActivity implements 
GooglePlayServicesClient.Connection...
Configure a LocationRequest 
mLocationRequest = LocationRequest.create(); 
mLocationRequest.setPriority(LocationRequest.PR...
Callback to LocationListener 
public class MainActivity extends FragmentActivity implements LocationListener { ... 
@Overr...
Register a pending intent 
PendingIntent bglocationUpdate = PendingIntent.getService(context, 0, 
new Intent(context, Back...
AlarmManager works best for us 
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, 
AlarmManagerLocat...
Other tricks
Activity Monitoring 
switch(activityType) { 
case DetectedActivity.IN_VEHICLE: 
return "in_vehicle"; 
case DetectedActivit...
Watch the battery life in your code 
<receiver android:name=".receivers.PowerStateChangedReceiver"> 
<intent-filter> 
<act...
Geofence 
mLocationClient.addGeofences(mCurrentGeofences, pendingIntent, this); 
if ((transitionType == Geofence.GEOFENCE_...
Upcoming
Battery Historian 
Project Volta 
$ adb shell dumpsys batterystats --charged <package-name> 
$ adb shell dumpsys batteryst...
Future explorations 
● BLE devices 
● Other nearby phones 
● Photo uploads 
● Android L - Job Scheduler
Thanks 
Questions? 
Arun Nagarajan 
google.com/+ArunNagarajan @entaq 
arun@isapp.com
Upcoming SlideShare
Loading in …5
×

Battery Efficient Location Services

1,050 views

Published on

DroidCon - NYC 2014 Presentation

Published in: Technology
  • Be the first to comment

Battery Efficient Location Services

  1. 1. Battery Efficient Location Services google.com/+ArunNagarajan @entaq NYC Droidcon, 9/21/14
  2. 2. About me Arun Nagarajan (@entaq) Currently Founding Lead Engineer, funded stealth startup (NYC) We are hiring! Email me at arun@isapp.com Previously 2 yrs at Google - Tech Lead, Developer Platform 9 yrs at Verivo Software (Boston) - VP of Architecture
  3. 3. Background
  4. 4. 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
  5. 5. Three variables to consider ● Power ● Accuracy ● Coverage
  6. 6. Various options Source - developers.google.com/events/io/sessions/325337477
  7. 7. Battery cost is real Source - developers.google.com/events/io/sessions/325337477
  8. 8. What takes up more power? ● CPU ● Display ● GPS ● WiFi What’s your guess?
  9. 9. What takes up more power? ● CPU ~ 300 milliwatts ● Display ~ 350 milliwatts ● GPS ~100 milliwatts ● WiFI ~30 milliwatts
  10. 10. User first
  11. 11. 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
  12. 12. Tools the user has ● Battery UI ● GPS indicator ○ Some Samsung devices blink indicator!
  13. 13. But there is more
  14. 14. Critical APIs
  15. 15. Key Resources d.android.com/training/location/index.html d.android.com/guide/topics/location/index.html d.android.com/guide/topics/sensors/index.html
  16. 16. Simple/Synchronous way public class MainActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener { ... // Global variable to hold the current location Location mCurrentLocation; ... mCurrentLocation = mLocationClient.getLastLocation(); ... }
  17. 17. Configure a LocationRequest mLocationRequest = 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);
  18. 18. Callback to LocationListener public class MainActivity extends FragmentActivity implements LocationListener { ... @Override public void onLocationChanged(Location location) { String msg = "Updated Location: " + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude()); } ... }
  19. 19. Register a pending intent PendingIntent bglocationUpdate = PendingIntent.getService(context, 0, new Intent(context, BackgroundLocationUpdateService.class), PendingIntent.FLAG_UPDATE_CURRENT); locationClient.requestLocationUpdates(locationRequest,bglocationUpdate); locationClient.disconnect();
  20. 20. AlarmManager works best for us PendingIntent 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); }
  21. 21. Other tricks
  22. 22. 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";
  23. 23. Watch the battery life in your code <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> boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW);
  24. 24. Geofence mLocationClient.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(); } }
  25. 25. Upcoming
  26. 26. 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
  27. 27. Future explorations ● BLE devices ● Other nearby phones ● Photo uploads ● Android L - Job Scheduler
  28. 28. Thanks Questions? Arun Nagarajan google.com/+ArunNagarajan @entaq arun@isapp.com

×