How to enable sync without syncing every few seconds

安稳与你 提交于 2019-12-19 04:07:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!