I have an app linked with a Web service that uses AccountManager and Sync to allow users to log in to their account through the app and sync their data (this is custom data, not contacts or anything else used by Google accounts). AFAIK, I'm doing everything the way I'm supposed to. When the user logs in through our API, I save their account to the Android AccountManager:
AccountManager am = AccountManager.get(context);
m.addAccountExplicitly(new Account(username, ACCOUNT_TYPE), account_id, user_data);
I have defined ACCOUNT_TYPE to just be the package name of the app, which should be sufficient to distinguish it from any other account type.
As soon as the account is added, I set it for automatic sync:
ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), SYNC_FREQUENCY_SECONDS);
I have defined SYNC_FREQUENCY_SECONDS as 86400 (i.e., 1 day).
Similarly, if the user logs out, I just retrieve their account from AccountManager and call removeAccount() to remove it. Nice and simple.
The Sync is carried out through my own code, because it is all custom data that is being synced, and that all works just fine. However, what's driving me crazy is that whenever I use this code to add or remove an account (of my own type), it immediately triggers a sync of all Google accounts on the device. This means that, before it syncs my data, it first has to sync all email, calendar data, contacts, Google Play Magazines, Google+ data and pictures, etc., etc., etc. This can take several minutes, during which my app is just patiently waiting its turn to sync data. It's extremely frustrating, and it's going to be hard for my users to understand why it takes so long to sync their data once they log in (not to mention the fact that, when it's syncing that much data, the phone has a tendency to slow down almost to the point of unresponsiveness).
Am I doing something wrong here? Is there something in my code that inadvertently tells Android, "Hey, go and sync all of your Google data now!"? Or is it something inherent in the Accounts/Sync framework of Google that I just have to put up with?
Go to “Settings” > “Users and accounts“. Swipe down and toggle on “Automatically sync data“. The following applies whether you are using Oreo or another Android version. If there are certain things of an app you can to unSync, you can.
I think (though I am not 100% sure) that this is a power-saving feature; it is more battery-efficient to send one long transmission rather than several shorter transmissions because the radio stays on in a standby power state for some time after the end of a transmission, and so sending a single long transmission avoids staying in standby (essentially wasting power) more than once. So I am guessing that the syncing infrastructure tries to batch together as much syncing as it possibly can.
If the concern is the latency of the first-time sync, perhaps you could manually request a sync for the first-time sync with a more immediate time in addition to the periodic automatic sync?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With