SlideShare una empresa de Scribd logo
1 de 28
Descargar para leer sin conexión
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 me at arun@isapp.com 
Previously 
2 yrs at Google - Tech Lead, Developer Platform 
9 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.html 
d.android.com/guide/topics/sensors/index.html
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(); 
... 
}
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);
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()); 
} 
... 
}
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();
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); 
}
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 
<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);
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(); 
} 
}
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
Thanks 
Questions? 
Arun Nagarajan 
google.com/+ArunNagarajan @entaq 
arun@isapp.com

Más contenido relacionado

Similar a Battery Efficient Location Services

097 smart devices-con_las_aplicaciones_de_gestión
097 smart devices-con_las_aplicaciones_de_gestión097 smart devices-con_las_aplicaciones_de_gestión
097 smart devices-con_las_aplicaciones_de_gestión
GeneXus
 
Windows Phone 8 - 15 Location and Maps
Windows Phone 8 - 15 Location and MapsWindows Phone 8 - 15 Location and Maps
Windows Phone 8 - 15 Location and Maps
Oliver Scheer
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
Alexey Buzdin
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
C.T.Co
 
Mobile Software Engineering Crash Course - C04 Android Cont.
Mobile Software Engineering Crash Course - C04 Android Cont.Mobile Software Engineering Crash Course - C04 Android Cont.
Mobile Software Engineering Crash Course - C04 Android Cont.
Mohammad Shaker
 

Similar a Battery Efficient Location Services (20)

097 smart devices-con_las_aplicaciones_de_gestión
097 smart devices-con_las_aplicaciones_de_gestión097 smart devices-con_las_aplicaciones_de_gestión
097 smart devices-con_las_aplicaciones_de_gestión
 
Develop a native application that uses GPS location.pptx
Develop a native application that uses GPS location.pptxDevelop a native application that uses GPS location.pptx
Develop a native application that uses GPS location.pptx
 
Developing Windows Phone Apps with Maps and Location Services
Developing Windows Phone Apps with Maps and Location ServicesDeveloping Windows Phone Apps with Maps and Location Services
Developing Windows Phone Apps with Maps and Location Services
 
Background Life with Android O and beyond - Yonatan Levin, KolGene
Background Life with Android O and beyond - Yonatan Levin, KolGeneBackground Life with Android O and beyond - Yonatan Levin, KolGene
Background Life with Android O and beyond - Yonatan Levin, KolGene
 
Boldly Go Where No Man Has Gone Before. Explore Geo on iPhone & Android
Boldly Go Where No Man Has Gone Before. Explore Geo on iPhone & AndroidBoldly Go Where No Man Has Gone Before. Explore Geo on iPhone & Android
Boldly Go Where No Man Has Gone Before. Explore Geo on iPhone & Android
 
Scaling Experimentation & Data Capture at Grab
Scaling Experimentation & Data Capture at GrabScaling Experimentation & Data Capture at Grab
Scaling Experimentation & Data Capture at Grab
 
Advanced android app development
Advanced android app developmentAdvanced android app development
Advanced android app development
 
How to use geolocation in react native apps
How to use geolocation in react native appsHow to use geolocation in react native apps
How to use geolocation in react native apps
 
Functional, Type-safe, Testable Microservices with ZIO and gRPC
Functional, Type-safe, Testable Microservices with ZIO and gRPCFunctional, Type-safe, Testable Microservices with ZIO and gRPC
Functional, Type-safe, Testable Microservices with ZIO and gRPC
 
"Enabling Googley microservices with gRPC" at JEEConf 2017
"Enabling Googley microservices with gRPC" at JEEConf 2017"Enabling Googley microservices with gRPC" at JEEConf 2017
"Enabling Googley microservices with gRPC" at JEEConf 2017
 
Android Pro Recipes
Android Pro RecipesAndroid Pro Recipes
Android Pro Recipes
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
Windows Phone 8 - 15 Location and Maps
Windows Phone 8 - 15 Location and MapsWindows Phone 8 - 15 Location and Maps
Windows Phone 8 - 15 Location and Maps
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Android Meetup Slovenia #2 - Making your app location-aware
Android Meetup Slovenia #2 - Making your app location-awareAndroid Meetup Slovenia #2 - Making your app location-aware
Android Meetup Slovenia #2 - Making your app location-aware
 
Infinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #9 - Making your app location-awareInfinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #9 - Making your app location-aware
 
Android Location-based應用開發分享
Android Location-based應用開發分享Android Location-based應用開發分享
Android Location-based應用開發分享
 
Mobile Software Engineering Crash Course - C04 Android Cont.
Mobile Software Engineering Crash Course - C04 Android Cont.Mobile Software Engineering Crash Course - C04 Android Cont.
Mobile Software Engineering Crash Course - C04 Android Cont.
 
"Enabling Googley microservices with gRPC" at JDK.IO 2017
"Enabling Googley microservices with gRPC" at JDK.IO 2017"Enabling Googley microservices with gRPC" at JDK.IO 2017
"Enabling Googley microservices with gRPC" at JDK.IO 2017
 

Más de Arun Nagarajan (8)

Google Maps on iOS with Swift
Google Maps on iOS with SwiftGoogle Maps on iOS with Swift
Google Maps on iOS with Swift
 
Animations & swift
Animations & swiftAnimations & swift
Animations & swift
 
CheckMark - Code the Deal hackathon
CheckMark - Code the Deal hackathonCheckMark - Code the Deal hackathon
CheckMark - Code the Deal hackathon
 
Mongo DB Hackday Apr 28 2012
Mongo DB Hackday Apr 28 2012Mongo DB Hackday Apr 28 2012
Mongo DB Hackday Apr 28 2012
 
Integrate Google Drive with Google Apps Script
Integrate Google Drive with Google Apps ScriptIntegrate Google Drive with Google Apps Script
Integrate Google Drive with Google Apps Script
 
Android L Preview - Recents Screen + API
Android L Preview  - Recents Screen + APIAndroid L Preview  - Recents Screen + API
Android L Preview - Recents Screen + API
 
CloudKit
CloudKitCloudKit
CloudKit
 
node.js on Google Compute Engine
node.js on Google Compute Enginenode.js on Google Compute Engine
node.js on Google Compute Engine
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

Battery Efficient Location Services

  • 1. Battery Efficient Location Services google.com/+ArunNagarajan @entaq NYC Droidcon, 9/21/14
  • 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
  • 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. Three variables to consider ● Power ● Accuracy ● Coverage
  • 6. Various options Source - developers.google.com/events/io/sessions/325337477
  • 7. Battery cost is real Source - developers.google.com/events/io/sessions/325337477
  • 8. What takes up more power? ● CPU ● Display ● GPS ● WiFi What’s your guess?
  • 9. What takes up more power? ● CPU ~ 300 milliwatts ● Display ~ 350 milliwatts ● GPS ~100 milliwatts ● WiFI ~30 milliwatts
  • 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. Tools the user has ● Battery UI ● GPS indicator ○ Some Samsung devices blink indicator!
  • 13. But there is more
  • 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. 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. 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. 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. 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. 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); }
  • 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. 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. 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(); } }
  • 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. Future explorations ● BLE devices ● Other nearby phones ● Photo uploads ● Android L - Job Scheduler
  • 28. Thanks Questions? Arun Nagarajan google.com/+ArunNagarajan @entaq arun@isapp.com