问题
I have used Google's Android developer documentation (http://developer.android.com/training/sync-adapters) to create a syncing system.
My app now has an entry in Android Settings under Accounts. When I tap this entry, I see the sync adaptor I have configured. Everything looks ok except that the box is unchecked and it says "Sync is OFF".
I have Googled and Stackoverflowed and there seems to be one way to enable sync, as explained here: https://stackoverflow.com/a/5279191/127434. The answer at that link says to call ContentResolver.setSyncAutomatically(account, authority, true)
.
However, Google's docs (http://developer.android.com/training/sync-adapters/running-sync-adapter.html#RunByNetwork) say:
When a network connection is available, the Android system sends out a message every few seconds to keep the device's TCP/IP connection open. This message also goes to the ContentResolver of each app. By calling setSyncAutomatically(), you can run the sync adapter whenever the ContentResolver receives the message.
But I don't want to sync every few seconds. (I'd like to use ContentResolver.addPeriodicSync
to schedule a sync a few times an hour.)
So how can I enable syncing for my adaptor without having it sync every few seconds?
回答1:
You should add ContentResolver.setMasterSyncAutomatically(true); to your code. Without this the setSyncAutomatically will be ignored.
Read the documentation [here] (http://developer.android.com/reference/android/content/ContentResolver.html#setMasterSyncAutomatically(boolean))
来源:https://stackoverflow.com/questions/22206596/how-to-enable-sync-without-syncing-every-few-seconds