$30 off During Our Annual Pro Sale. View Details »

Doze Mode & App Standby in Android M

David Vávra
November 13, 2015

Doze Mode & App Standby in Android M

Android Marshmallow brings major changes to the platform which can limit functionality of existing apps. If you are doing any background work in your app, your app will be affected by Doze &App Standby in favour of longer battery life. Let’s see what APIs will be limited and what are the best practices for scheduling background tasks.

David Vávra

November 13, 2015
Tweet

More Decks by David Vávra

Other Decks in Programming

Transcript

  1. Doze Mode
    & App Standby
    in Android M
    David Vávra, GDE
    +David Vávra
    @destil

    View Slide

  2. Why?

    View Slide

  3. Doze mode
    Triggered when:
    not charging && stationary && screen is off

    View Slide

  4. App Standby
    Per-app Doze
    App is in standby when:
    no Activities && no notifications && not charging
    App is woken once a day for a brief time

    View Slide

  5. Android Scheduling APIs SyncManager

    View Slide

  6. Doze Mode SyncManager
    no wakelocks
    no network
    10 s of network & wakelock, only once in 15 mins
    Device exits Doze

    View Slide

  7. App Standby SyncManager
    wakelocks work
    no network

    View Slide

  8. The golden ticket
    High-priority GCM message.
    But:
    Doesn’t work offline.
    You might use different provider (Parse, SIP)
    No SLA

    View Slide

  9. Opting out
    partial wakelock for Doze, no App Standby
    Intent intent = new Intent(Settings.
    ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    intent.setData(Uri.parse("package:com.example.app"));
    startActivity(intent);
    Intent intent = new Intent(Settings.
    REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:com.example.app"));
    startActivity(intent);
    android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
    Only valid use-cases! Break core feature, can’t use GCM

    View Slide

  10. Testing
    $ adb shell dumpsys battery unplug
    $ adb shell dumpsys deviceidle step
    $ adb shell am set-inactive true
    $ adb shell am get-inactive

    View Slide

  11. More details
    Detect whether the device is in Doze with
    PowerManager#isDeviceIdleMode()
    Detect is your app is whitelisted with
    isIgnoringBatteryOptimizations(String package)
    If you schedule more alarms, they are all triggered after
    exiting Doze.
    Doze/App Standby doesn’t work on devices without
    Google Play Services.

    View Slide

  12. More details
    Maintenance windows: 5-10 mins, period 1, 2, 4, 6, 6, 6 hours.
    ContentProviders in the dozed app should work.
    Doze disables before user is woken by alarm clock to let apps
    synchronize.
    Apps with foreground service are not affected by Doze.
    You can register to android.os.action.
    DEVICE_IDLE_MODE_CHANGED

    View Slide

  13. But…
    that will break my app!
    Use-cases & solutions

    View Slide

  14. Use-case #1: Instant Messenger
    The app should notify me
    instantly when I receive a new
    message.
    Solution:
    High-priority GCM message.
    or white-listing

    View Slide

  15. Use-case #2: SMS Manager
    The app should notify about
    incoming SMS immediately.
    Solution:
    Some Broadcasts like
    SMS_RECEIVED works.

    View Slide

  16. Use-case #3: Sleep Tracker
    The app should monitor user’s
    sleep using accelerometer and
    microphone.
    Solution:
    Ask user to plug it into charger
    Whitelist & Foreground service
    & Wakelock (buggy)

    View Slide

  17. Use-case 4#: Periodic Backup
    The app should backup user’s
    photos periodically to the
    cloud.
    Solution:
    GCM Network Manager with
    network & charge
    requirement
    Foreground service for
    downloading

    View Slide

  18. Use-case #5: Podcast Downloader
    The app should download
    subscribed podcasts at night so
    they are ready for offline
    listening when I drive to work.
    Solution:
    GCM Network Manager with
    network requirement
    Foreground service for
    downloading
    Reschedule if fails

    View Slide

  19. Use-case #6: Anti-Theft
    The app should be invisible and
    should allow remove control of
    the device.
    Solution:
    Whitelisting
    Use high-priority GCM & SMS
    to control device

    View Slide

  20. Sources
    https://developer.android.com/preview/features/power-mgmt.html
    https://developers.google.com/cloud-messaging/network-manager
    https://commonsware.com/blog/2015/06/03/random-musing-m-developer-
    preview-ugly-part-one.html
    https://plus.google.com/+AndroidDevelopers/posts/GdNrQciPwqo
    https://plus.google.com/u/0/+TomasHubalek/posts/NXZdkR29h9m
    https://code.google.com/p/android-developer-preview/issues/detail?id=2233

    View Slide

  21. We welcome talented Android Developers
    To Prague - nice relocation package :)
    http://avast.jobs.cz or ping me at [email protected]

    View Slide

  22. Q&A
    +David Vávra
    @destil

    View Slide