Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android battery performance optimisation

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Android battery performance optimisation

This presentation uncovers what are the main battery drain reasons and how to overcome them. Second part of the presentation gives useful tips and tricks how to develop an app keeping battery performance in mind.
https://youtu.be/ynlQq1hB3Ks

Avatar for Sergii Kozyrev

Sergii Kozyrev

November 23, 2017
Tweet

More Decks by Sergii Kozyrev

Other Decks in Programming

Transcript

  1. Screen Fixed •Remove WakeLocks (unless it’s payment) •WakeLocks/Sound/Flash – don’t

    forget to “close the door” and remove •Need bright screen – learn from PlanetaKino
  2. Location Scanning Fixed •GPS – use GeoFencing (100points, 2 min)

    •Wi-Fi and BLE – ActivityLifecycleCallbacks as BatterySaver
  3. Simple but powerful compile 'com.squareup.retrofit2:retrofit: 2.3.0'
 compile 'com.squareup.okhttp3:okhttp: 3.4.1' Retrofit

    retrofit = new Retrofit.Builder().client(new OkHttpClient.Builder().build())
 .baseUrl(getString(R.string.base_url))
 .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
 .build()
  4. Simple but powerful private boolean isNetworkConnected() {
 ConnectivityManager connectivityManager =


    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
 return (networkInfo != null && networkInfo.isConnected());
 } No connection, No request, no radio module power consumption, PROFIT!
  5. What we need to know? AppStandby Touches: Application Condition: App

    in background Sync allowed: once per day Doze Touches: Device Condition: Screen off & unplugged Sync allowed: exponential window Restricted: network, wakelocks, Alarm*, syncadapter, JobScheduler How to fit: FCM or exclusions (settings allowed for IM, Task automation, Peripheral Companion)
  6. Tag Network Requests if (Build.VERSION.SDK_INT >= 14) {
 try {


    TrafficStats.setThreadStatsTag(USER_INITIATED|APP_INITIATED| SERVER_INITIATED);
 // make network request using HttpClient.execute()
 } finally {
 TrafficStats.clearThreadStatsTag();
 }
 }
  7. Think and Act! User-initiated traffic App-initiated traffic Server-initiated traffic Pre-fetch

    Network Data Use batch-requests (multi-payload) Batch and Schedule Requests Push(Gcm/Fcm), not long polling
  8. SyncAdapter – hard to implement and configure(account, ContentProvider)
 
 JobScheduler

    - Minimum API 21
 
 Solution … Firebase JobDispatcher 
 
 DEMO