Search code examples
androidandroid-syncadapter

SyncAdapter stays awake for hours on end


My Android app uses a SyncAdapter to sync with my API service. I use delayUntil to tell it to only sync every half an hour. However, some users are reporting incidents where it never goes to sleep again once it's done, draining the phone battery (e.g. the app shows up in the device's battery page showing a "Keep awake" of 13 hours+).

I'm also certain there's no infinite loops in the onPerformSync method - it makes a few HTTP calls and updates the database and that's about it.

Has anybody else ever experienced anything like this, and if so, what was behind it?


Solution

  • Look in your Application onCreate method. The mistake I made was assuming that the Application only ever starts up when the user interacts with your app, but in fact the Application starts every time the SyncAdapter does its stuff. In my case the Application's onCreate method was telling Dropbox to start syncing, and the code designed to make Dropbox stop syncing never kicked in (it happened when the last Activity was shut down - which of course never happened because there were no Activities when just the SyncAdapter was running) so it never went to sleep again.