SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
Coding for Life--Battery
Life, That Is
Jeff Sharkey
May 27, 2009

Post your questions for this talk on Google Moderator:
code.google.com/events/io/questions
Why does this matter?
 Phones primarily run on battery power, and each device
 has a "battery budget"
    When it's gone, it's gone
    Apps need to work together to be good citizens of that
    shared resource
    Current measured in mA, battery capacity in mAh

 HTC Dream: 1150mAh
 HTC Magic: 1350mAh
 Samsung I7500: 1500mAh
 Asus Eee PC: 5800mAh
Where does it all go?


          Source: Values measured using an industrial power
          monitor at 5kHz sampling rate, and taking average
          power with lowest standard deviation.
Where does it all go?

 How do these numbers add up in real life?
    Watching YouTube: 340mA = 3.4 hours
    Browsing 3G web: 225mA = 5 hours
    Typical usage: 42mA average = 32 hours
    EDGE completely idle: 5mA = 9.5 days
    Airplane mode idle: 2mA = 24 days
What costs the most?

 Waking up in the background when the phone would
 otherwise be sleeping
    App wakes up every 10 minutes to update
    Takes about 8 seconds to update, 350mA
 Cost during a given hour:
    3600 seconds * 5mA = 5mAh resting
    6 times * 8 sec * 350 mA = 4.6mAh updating
 Just one app waking up can trigger cascade
What costs the most?

 Bulk data transfer such as a 6MB song:
    EDGE (90kbps): 300mA * 9.1 min = 45 mAh
    3G (300kbps): 210mA * 2.7 min = 9.5 mAh
    WiFi (1Mbps): 330mA * 48 sec = 4.4 mAh
 Moving between cells/networks
    Radio ramps up to associate with new cell
    BroadcastIntents fired across system
 Parsing textual data, regex without JIT
How can we do better?
Networking
How can we do better?
Networking

 Check network connection, wait for 3G or WiFi


ConnectivityManager mConnectivity;
TelephonyManager mTelephony;

// Skip if no connection, or background data disabled
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if (info == null ||
        !mConnectivity.getBackgroundDataSetting()) {
    return false;
}
How can we do better?
Networking

 Check network connection, wait for 3G or WiFi

 // Only update if WiFi or 3G is connected and not roaming
 int netType = info.getType();
 int netSubtype = info.getSubtype();

 if (netType == ConnectivityManager.TYPE_WIFI) {
     return info.isConnected();
 } else if (netType == ConnectivityManager.TYPE_MOBILE
          && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
          && !mTelephony.isNetworkRoaming()) {
     return info.isConnected();
 } else {
     return false;
 }
How can we do better?
Networking

 Use an efficient data format and parser




                                    Source: Timings obtained by downloading and
                                    parsing a 6-item RSS feed repeatedly for 60
                                    seconds and averaging results.
How can we do better?
Networking

 Use an efficient data format and parser
    Use "stream" parsers instead of tree parsers
    Consider binary formats that can easily mix binary and
    text data into a single request
    Fewer round-trips to server for faster UX
How can we do better?
Networking

 Use GZIP for text data whenever possible
    Framework GZIP libs go directly to native code , and are
    perfect for streams
  import java.util.zip.GZIPInputStream;

  HttpGet request =
      new HttpGet("http://example.com/gzipcontent");
  HttpResponse resp =
      new DefaultHttpClient().execute(request);
  HttpEntity entity = response.getEntity();
  InputStream compressed = entity.getContent();
  InputStream rawData = new GZIPInputStream(compressed);
How can we do better?
Networking

 Use GZIP for text data whenever possible



                                     Source: Timings averaged over multiple trials of
                                     downloading 1800-item RSS feed of textual data.
How can we do better?
Foreground apps
How can we do better?
Foreground apps

 Wakelocks are costly if forgotten
    Pick the lowest level possible, and use specific
    timeouts to work around unforseen bugs
    Consider using android:keepScreenOn to ensure
    correctness

 <LinearLayout
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:keepScreenOn="true">
How can we do better?
Foreground apps

 Recycle Java objects, especially complex objects
    Yes, we have a GC, but usually better to just create
    less garbage that it has to clean up
        XmlPullParserFactory and BitmapFactory
        Matcher.reset(newString) for regex
        StringBuilder.setLength(0)
    Watch for synchronization issues, but can be safe when
    driven by UI thread
    Recycling strategies are used heavily in ListView
How can we do better?
Foreground apps

 Use coarse network location, it's much cheaper
    GPS: 25 seconds * 140mA = 1mAh
    Network: 2 seconds * 180mA = 0.1mAh
 1.5 uses AGPS when network available
 GPS time-to-fix varies wildly based on environment, and
 desired accuracy, and might outright fail
    Just like wake-locks, location updates can continue
    after onPause(), so make sure to unregister
    If all apps unregister correctly, user can leave GPS
    enabled in Settings
How can we do better?
Foreground apps

 Floating point math is expensive
    Using microdegrees when doing bulk geographic math
 // GeoPoint returns value 37392778, -122041944
 double lat = GeoPoint.getLatitudeE6() / 1E6;
 double lon = GeoPoint.getLongitudeE6() / 1E6;



    Caching values when doing DPI work with
    DisplayMetrics
 float density =
         getResources().getDisplayMetrics().density;
 int actualWidth =
         (int)(bitmap.getWidth() * density);
How can we do better?
Foreground apps

 Accelerometer/magnetic sensors
    Normal: 10mA (used for orientation detection)
    UI: 15mA (about 1 per second)
    Game: 80mA
    Fastest: 90mA
 Same cost for accelerometer, magnetic, orientation
 sensors on HTC Dream
How can we do better?
Background apps
How can we do better?
Background apps

 Services should be short-lived; these aren't daemons
    Each process costs 2MB and risks being
    killed/restarted as foreground apps need memory
    Otherwise, keep memory usage low so you're not the
    first target
 Trigger wake-up through AlarmManager or with <receiver>
 manifest elements
    stopSelf() when finished
How can we do better?
Background apps

 Start service using AlarmManager
    Use the _WAKEUP flags with caution
    App that updates every 30 minutes, but only when
    device is already awake
AlarmManager am = (AlarmManager)
        context.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(context, MyService.class);
PendingIntent pendingIntent =
        PendingIntent.getService(context, 0, intent, 0);

long interval = DateUtils.MINUTE_IN_MILLIS * 30;
long firstWake = System.currentTimeMillis() + interval;

am.setRepeating(AlarmManager.RTC,
        firstWake, interval, pendingIntent);
How can we do better?
Background apps

 Use setInexactRepeating() so the system can bin your
 update together with others
How can we do better?
Background apps

 Start your service using <receiver> in manifest
    Intent.ACTION_TIMEZONE_CHANGED
    ConnectivityManager.CONNECTIVITY_ACTION
    Intent.ACTION_DEVICE_STORAGE_LOW
    Intent.ACTION_BATTERY_LOW
    Intent.ACTION_MEDIA_MOUNTED
<receiver android:name=".ConnectivityReceiver">
    <intent-filter>
        <action android:name=
            "android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>
How can we do better?
Background apps

 Dynamically enabling/disabling <receiver> components in
 manifest, especially when no-ops
  <receiver android:name=".ConnectivityReceiver"
      android:enabled="false">
      ...
  </receiver>



 ComponentName receiver = new ComponentName(context,
         ConnectivityReceiver.class);
 PackageManager pm = context.getPackageManager();
 pm.setComponentEnabledSetting(receiver,
         PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
         PackageManager.DONT_KILL_APP);
How can we do better?
Background apps

 Checking current battery and network state before running
 a full update
 public void onCreate() {
     // Register for sticky broadcast and send default
     registerReceiver(mReceiver, mFilter);
     mHandler.sendEmptyMessageDelayed(MSG_BATT, 1000);
 }

 IntentFilter mFilter =
         new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

 BroadcastReceiver mReceiver = new BroadcastReceiver() {
     public void onReceive(Context context, Intent intent) {
         // Found sticky broadcast, so trigger update
         unregisterReceiver(mReceiver);
         mHandler.removeMessages(MSG_BATT);
         mHandler.obtainMessage(MSG_BATT, intent).sendToTarget();
     }
 };
Beyond 1.5
Users will be watching!

                    SpareParts has "Battery history"
                       1.5 is already keeping stats on
                       which apps are using CPU,
                       network, wakelocks
                       Simplified version coming in
                       future, and users will uninstall
                       apps that abuse battery
                    Consider giving users options for
                    battery usage, like update intervals,
                    and check the "no background data"
                    flag
Takeaways

Use an efficient parser and GZIP to make best use of
network and CPU resources
Services that sleep or poll are bad, use <receiver> and
AlarmManager instead
   Disable manifest elements when no-op
   Wake up along with everyone else (inexact alarms)
Wait for better network/battery for bulk transfers
Give users choices about background behavior
Q&A
Post your questions for this talk on Google Moderator:
code.google.com/events/io/questions
W 0300 codingfor_life-batterylifethatis

Weitere ähnliche Inhalte

Andere mochten auch

דרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדותדרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדותMenyNachman
 
Payroll servicesoverview
Payroll servicesoverviewPayroll servicesoverview
Payroll servicesoverviewjohndonley
 
Iit jee chem 10 2
Iit jee chem 10 2Iit jee chem 10 2
Iit jee chem 10 2swaaru
 
Expert tm syllabus beta version 041511
Expert tm syllabus beta version 041511Expert tm syllabus beta version 041511
Expert tm syllabus beta version 041511jicheng687
 
【Android终端安全分享】之android程序打包过程分析
【Android终端安全分享】之android程序打包过程分析【Android终端安全分享】之android程序打包过程分析
【Android终端安全分享】之android程序打包过程分析jicheng687
 
15 godzin do raju fin
15 godzin do raju fin15 godzin do raju fin
15 godzin do raju finNoemi Gryczko
 
Na czym polega niekonferencja?
Na czym polega niekonferencja?Na czym polega niekonferencja?
Na czym polega niekonferencja?Noemi Gryczko
 
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדותדרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדותMenyNachman
 
Announcements for march 20, 2011
Announcements for march 20, 2011Announcements for march 20, 2011
Announcements for march 20, 2011betollaf
 
Technologie mobilne w bibliotekach
Technologie mobilne w bibliotekachTechnologie mobilne w bibliotekach
Technologie mobilne w bibliotekachNoemi Gryczko
 
Моделирование растворимости в сверхкритическом диоксиде углерода с использова...
Моделирование растворимости в сверхкритическом диоксиде углерода с использова...Моделирование растворимости в сверхкритическом диоксиде углерода с использова...
Моделирование растворимости в сверхкритическом диоксиде углерода с использова...Serg Maksimov
 

Andere mochten auch (13)

דרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדותדרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
 
Payroll servicesoverview
Payroll servicesoverviewPayroll servicesoverview
Payroll servicesoverview
 
test
testtest
test
 
Iit jee chem 10 2
Iit jee chem 10 2Iit jee chem 10 2
Iit jee chem 10 2
 
Expert tm syllabus beta version 041511
Expert tm syllabus beta version 041511Expert tm syllabus beta version 041511
Expert tm syllabus beta version 041511
 
【Android终端安全分享】之android程序打包过程分析
【Android终端安全分享】之android程序打包过程分析【Android终端安全分享】之android程序打包过程分析
【Android终端安全分享】之android程序打包过程分析
 
Unconference
Unconference Unconference
Unconference
 
15 godzin do raju fin
15 godzin do raju fin15 godzin do raju fin
15 godzin do raju fin
 
Na czym polega niekonferencja?
Na czym polega niekonferencja?Na czym polega niekonferencja?
Na czym polega niekonferencja?
 
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדותדרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
דרום אדום - פריחת הכלניות בנגב - טיולים ואגדות
 
Announcements for march 20, 2011
Announcements for march 20, 2011Announcements for march 20, 2011
Announcements for march 20, 2011
 
Technologie mobilne w bibliotekach
Technologie mobilne w bibliotekachTechnologie mobilne w bibliotekach
Technologie mobilne w bibliotekach
 
Моделирование растворимости в сверхкритическом диоксиде углерода с использова...
Моделирование растворимости в сверхкритическом диоксиде углерода с использова...Моделирование растворимости в сверхкритическом диоксиде углерода с использова...
Моделирование растворимости в сверхкритическом диоксиде углерода с использова...
 

Ähnlich wie W 0300 codingfor_life-batterylifethatis

DevoxxUK: Optimizating Application Performance on Kubernetes
DevoxxUK: Optimizating Application Performance on KubernetesDevoxxUK: Optimizating Application Performance on Kubernetes
DevoxxUK: Optimizating Application Performance on KubernetesDinakar Guniguntala
 
Open Source XMPP for Cloud Services
Open Source XMPP for Cloud ServicesOpen Source XMPP for Cloud Services
Open Source XMPP for Cloud Servicesmattjive
 
Giga Spaces Data Grid / Data Caching Overview
Giga Spaces Data Grid / Data Caching OverviewGiga Spaces Data Grid / Data Caching Overview
Giga Spaces Data Grid / Data Caching Overviewjimliddle
 
Power optimization for Android apps
Power optimization for Android appsPower optimization for Android apps
Power optimization for Android appsXavier Hallade
 
Introduction to Magento Optimization
Introduction to Magento OptimizationIntroduction to Magento Optimization
Introduction to Magento OptimizationFabio Daniele
 
Gatling - Bordeaux JUG
Gatling - Bordeaux JUGGatling - Bordeaux JUG
Gatling - Bordeaux JUGslandelle
 
Strata Singapore: Gearpump Real time DAG-Processing with Akka at Scale
Strata Singapore: GearpumpReal time DAG-Processing with Akka at ScaleStrata Singapore: GearpumpReal time DAG-Processing with Akka at Scale
Strata Singapore: Gearpump Real time DAG-Processing with Akka at ScaleSean Zhong
 
Android Battery optimization Android Apps
Android Battery optimization Android AppsAndroid Battery optimization Android Apps
Android Battery optimization Android AppsSingsys Pte Ltd
 
Android Pro Tips - IO 13 reloaded Event
Android Pro Tips - IO 13 reloaded EventAndroid Pro Tips - IO 13 reloaded Event
Android Pro Tips - IO 13 reloaded EventRan Nachmany
 
Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Matthew McCullough
 
OGCE RT Rroject Review
OGCE RT Rroject ReviewOGCE RT Rroject Review
OGCE RT Rroject Reviewmarpierc
 
OGCE Review for Indiana University Research Technologies
OGCE Review for Indiana University Research TechnologiesOGCE Review for Indiana University Research Technologies
OGCE Review for Indiana University Research Technologiesmarpierc
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAnuradha Weeraman
 
Scaling Experimentation & Data Capture at Grab
Scaling Experimentation & Data Capture at GrabScaling Experimentation & Data Capture at Grab
Scaling Experimentation & Data Capture at GrabRoman
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey J On The Beach
 
Dataservices: Processing (Big) Data the Microservice Way
Dataservices: Processing (Big) Data the Microservice WayDataservices: Processing (Big) Data the Microservice Way
Dataservices: Processing (Big) Data the Microservice WayQAware GmbH
 
Pandora FMS: Windows Phone 7 Agent
Pandora FMS: Windows Phone 7 AgentPandora FMS: Windows Phone 7 Agent
Pandora FMS: Windows Phone 7 AgentPandora FMS
 
Android app performance
Android app performanceAndroid app performance
Android app performanceSaksham Keshri
 

Ähnlich wie W 0300 codingfor_life-batterylifethatis (20)

DevoxxUK: Optimizating Application Performance on Kubernetes
DevoxxUK: Optimizating Application Performance on KubernetesDevoxxUK: Optimizating Application Performance on Kubernetes
DevoxxUK: Optimizating Application Performance on Kubernetes
 
Open Source XMPP for Cloud Services
Open Source XMPP for Cloud ServicesOpen Source XMPP for Cloud Services
Open Source XMPP for Cloud Services
 
Giga Spaces Data Grid / Data Caching Overview
Giga Spaces Data Grid / Data Caching OverviewGiga Spaces Data Grid / Data Caching Overview
Giga Spaces Data Grid / Data Caching Overview
 
Power optimization for Android apps
Power optimization for Android appsPower optimization for Android apps
Power optimization for Android apps
 
Introduction to Magento Optimization
Introduction to Magento OptimizationIntroduction to Magento Optimization
Introduction to Magento Optimization
 
Gatling - Bordeaux JUG
Gatling - Bordeaux JUGGatling - Bordeaux JUG
Gatling - Bordeaux JUG
 
Strata Singapore: Gearpump Real time DAG-Processing with Akka at Scale
Strata Singapore: GearpumpReal time DAG-Processing with Akka at ScaleStrata Singapore: GearpumpReal time DAG-Processing with Akka at Scale
Strata Singapore: Gearpump Real time DAG-Processing with Akka at Scale
 
Android Battery optimization Android Apps
Android Battery optimization Android AppsAndroid Battery optimization Android Apps
Android Battery optimization Android Apps
 
Android Pro Tips - IO 13 reloaded Event
Android Pro Tips - IO 13 reloaded EventAndroid Pro Tips - IO 13 reloaded Event
Android Pro Tips - IO 13 reloaded Event
 
Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2
 
Android session-5-sajib
Android session-5-sajibAndroid session-5-sajib
Android session-5-sajib
 
OGCE RT Rroject Review
OGCE RT Rroject ReviewOGCE RT Rroject Review
OGCE RT Rroject Review
 
OGCE Review for Indiana University Research Technologies
OGCE Review for Indiana University Research TechnologiesOGCE Review for Indiana University Research Technologies
OGCE Review for Indiana University Research Technologies
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the Trenches
 
Scaling Experimentation & Data Capture at Grab
Scaling Experimentation & Data Capture at GrabScaling Experimentation & Data Capture at Grab
Scaling Experimentation & Data Capture at Grab
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey
 
Dataservices: Processing (Big) Data the Microservice Way
Dataservices: Processing (Big) Data the Microservice WayDataservices: Processing (Big) Data the Microservice Way
Dataservices: Processing (Big) Data the Microservice Way
 
Pandora FMS: Windows Phone 7 Agent
Pandora FMS: Windows Phone 7 AgentPandora FMS: Windows Phone 7 Agent
Pandora FMS: Windows Phone 7 Agent
 
Android app performance
Android app performanceAndroid app performance
Android app performance
 
Balancing Power & Performance Webinar
Balancing Power & Performance WebinarBalancing Power & Performance Webinar
Balancing Power & Performance Webinar
 

Kürzlich hochgeladen

Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funneljen_giacalone
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...SUHANI PANDEY
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Servicearoranaina404
 
The Art of Batik, template ppt aesthetic
The Art of Batik, template ppt aestheticThe Art of Batik, template ppt aesthetic
The Art of Batik, template ppt aestheticTiaFebriani
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja Nehwal
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...Call Girls in Nagpur High Profile
 
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...Pooja Nehwal
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfAmirYakdi
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call GirlsCBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girlsmodelanjalisharma4
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...kumaririma588
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Call Girls in Nagpur High Profile
 

Kürzlich hochgeladen (20)

Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funnel
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
 
The Art of Batik, template ppt aesthetic
The Art of Batik, template ppt aestheticThe Art of Batik, template ppt aesthetic
The Art of Batik, template ppt aesthetic
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
 
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call GirlsCBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
 

W 0300 codingfor_life-batterylifethatis

  • 1.
  • 2. Coding for Life--Battery Life, That Is Jeff Sharkey May 27, 2009 Post your questions for this talk on Google Moderator: code.google.com/events/io/questions
  • 3. Why does this matter? Phones primarily run on battery power, and each device has a "battery budget" When it's gone, it's gone Apps need to work together to be good citizens of that shared resource Current measured in mA, battery capacity in mAh HTC Dream: 1150mAh HTC Magic: 1350mAh Samsung I7500: 1500mAh Asus Eee PC: 5800mAh
  • 4. Where does it all go? Source: Values measured using an industrial power monitor at 5kHz sampling rate, and taking average power with lowest standard deviation.
  • 5. Where does it all go? How do these numbers add up in real life? Watching YouTube: 340mA = 3.4 hours Browsing 3G web: 225mA = 5 hours Typical usage: 42mA average = 32 hours EDGE completely idle: 5mA = 9.5 days Airplane mode idle: 2mA = 24 days
  • 6. What costs the most? Waking up in the background when the phone would otherwise be sleeping App wakes up every 10 minutes to update Takes about 8 seconds to update, 350mA Cost during a given hour: 3600 seconds * 5mA = 5mAh resting 6 times * 8 sec * 350 mA = 4.6mAh updating Just one app waking up can trigger cascade
  • 7. What costs the most? Bulk data transfer such as a 6MB song: EDGE (90kbps): 300mA * 9.1 min = 45 mAh 3G (300kbps): 210mA * 2.7 min = 9.5 mAh WiFi (1Mbps): 330mA * 48 sec = 4.4 mAh Moving between cells/networks Radio ramps up to associate with new cell BroadcastIntents fired across system Parsing textual data, regex without JIT
  • 8. How can we do better? Networking
  • 9. How can we do better? Networking Check network connection, wait for 3G or WiFi ConnectivityManager mConnectivity; TelephonyManager mTelephony; // Skip if no connection, or background data disabled NetworkInfo info = mConnectivity.getActiveNetworkInfo(); if (info == null || !mConnectivity.getBackgroundDataSetting()) { return false; }
  • 10. How can we do better? Networking Check network connection, wait for 3G or WiFi // Only update if WiFi or 3G is connected and not roaming int netType = info.getType(); int netSubtype = info.getSubtype(); if (netType == ConnectivityManager.TYPE_WIFI) { return info.isConnected(); } else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { return info.isConnected(); } else { return false; }
  • 11. How can we do better? Networking Use an efficient data format and parser Source: Timings obtained by downloading and parsing a 6-item RSS feed repeatedly for 60 seconds and averaging results.
  • 12. How can we do better? Networking Use an efficient data format and parser Use "stream" parsers instead of tree parsers Consider binary formats that can easily mix binary and text data into a single request Fewer round-trips to server for faster UX
  • 13. How can we do better? Networking Use GZIP for text data whenever possible Framework GZIP libs go directly to native code , and are perfect for streams import java.util.zip.GZIPInputStream; HttpGet request = new HttpGet("http://example.com/gzipcontent"); HttpResponse resp = new DefaultHttpClient().execute(request); HttpEntity entity = response.getEntity(); InputStream compressed = entity.getContent(); InputStream rawData = new GZIPInputStream(compressed);
  • 14. How can we do better? Networking Use GZIP for text data whenever possible Source: Timings averaged over multiple trials of downloading 1800-item RSS feed of textual data.
  • 15. How can we do better? Foreground apps
  • 16. How can we do better? Foreground apps Wakelocks are costly if forgotten Pick the lowest level possible, and use specific timeouts to work around unforseen bugs Consider using android:keepScreenOn to ensure correctness <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:keepScreenOn="true">
  • 17. How can we do better? Foreground apps Recycle Java objects, especially complex objects Yes, we have a GC, but usually better to just create less garbage that it has to clean up XmlPullParserFactory and BitmapFactory Matcher.reset(newString) for regex StringBuilder.setLength(0) Watch for synchronization issues, but can be safe when driven by UI thread Recycling strategies are used heavily in ListView
  • 18. How can we do better? Foreground apps Use coarse network location, it's much cheaper GPS: 25 seconds * 140mA = 1mAh Network: 2 seconds * 180mA = 0.1mAh 1.5 uses AGPS when network available GPS time-to-fix varies wildly based on environment, and desired accuracy, and might outright fail Just like wake-locks, location updates can continue after onPause(), so make sure to unregister If all apps unregister correctly, user can leave GPS enabled in Settings
  • 19. How can we do better? Foreground apps Floating point math is expensive Using microdegrees when doing bulk geographic math // GeoPoint returns value 37392778, -122041944 double lat = GeoPoint.getLatitudeE6() / 1E6; double lon = GeoPoint.getLongitudeE6() / 1E6; Caching values when doing DPI work with DisplayMetrics float density = getResources().getDisplayMetrics().density; int actualWidth = (int)(bitmap.getWidth() * density);
  • 20. How can we do better? Foreground apps Accelerometer/magnetic sensors Normal: 10mA (used for orientation detection) UI: 15mA (about 1 per second) Game: 80mA Fastest: 90mA Same cost for accelerometer, magnetic, orientation sensors on HTC Dream
  • 21. How can we do better? Background apps
  • 22. How can we do better? Background apps Services should be short-lived; these aren't daemons Each process costs 2MB and risks being killed/restarted as foreground apps need memory Otherwise, keep memory usage low so you're not the first target Trigger wake-up through AlarmManager or with <receiver> manifest elements stopSelf() when finished
  • 23. How can we do better? Background apps Start service using AlarmManager Use the _WAKEUP flags with caution App that updates every 30 minutes, but only when device is already awake AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, MyService.class); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); long interval = DateUtils.MINUTE_IN_MILLIS * 30; long firstWake = System.currentTimeMillis() + interval; am.setRepeating(AlarmManager.RTC, firstWake, interval, pendingIntent);
  • 24. How can we do better? Background apps Use setInexactRepeating() so the system can bin your update together with others
  • 25. How can we do better? Background apps Start your service using <receiver> in manifest Intent.ACTION_TIMEZONE_CHANGED ConnectivityManager.CONNECTIVITY_ACTION Intent.ACTION_DEVICE_STORAGE_LOW Intent.ACTION_BATTERY_LOW Intent.ACTION_MEDIA_MOUNTED <receiver android:name=".ConnectivityReceiver"> <intent-filter> <action android:name= "android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
  • 26. How can we do better? Background apps Dynamically enabling/disabling <receiver> components in manifest, especially when no-ops <receiver android:name=".ConnectivityReceiver" android:enabled="false"> ... </receiver> ComponentName receiver = new ComponentName(context, ConnectivityReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
  • 27. How can we do better? Background apps Checking current battery and network state before running a full update public void onCreate() { // Register for sticky broadcast and send default registerReceiver(mReceiver, mFilter); mHandler.sendEmptyMessageDelayed(MSG_BATT, 1000); } IntentFilter mFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { // Found sticky broadcast, so trigger update unregisterReceiver(mReceiver); mHandler.removeMessages(MSG_BATT); mHandler.obtainMessage(MSG_BATT, intent).sendToTarget(); } };
  • 29. Users will be watching! SpareParts has "Battery history" 1.5 is already keeping stats on which apps are using CPU, network, wakelocks Simplified version coming in future, and users will uninstall apps that abuse battery Consider giving users options for battery usage, like update intervals, and check the "no background data" flag
  • 30. Takeaways Use an efficient parser and GZIP to make best use of network and CPU resources Services that sleep or poll are bad, use <receiver> and AlarmManager instead Disable manifest elements when no-op Wake up along with everyone else (inexact alarms) Wait for better network/battery for bulk transfers Give users choices about background behavior
  • 31. Q&A Post your questions for this talk on Google Moderator: code.google.com/events/io/questions