如何的Andr​​oid启用/禁用自动同步编程

问题描述:

我需要知道如何切换自动同步开启和关闭编程。

I need to know how to toggle auto sync on and off programmatically.

我认为你正在寻找

ContentResolver.setMasterSyncAutomatically(<boolean>);

说什么文档:

What docs says:

设定适用于所有的供应商主自动同步设置   和账户。如果这是假的,然后每提供商自动同步设置   被忽略。

Sets the master auto-sync setting that applies to all the providers and accounts. If this is false then the per-provider auto-sync setting is ignored.

此方法需要调用者持有许可   WRITE_SYNC_SETTINGS。

This method requires the caller to hold the permission WRITE_SYNC_SETTINGS.

所以,不要忘了添加到许可的manifest.xml:

So don't forget to add permission into manifest.xml:

<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

这应该禁用/启用所有同步。

This should disable / enable all the syncs.

@Sajmon:我更新了这个我觉得非常有用的答案(我在我的个人项目中使用这个)

@Sajmon: I updated this i think very useful answer (i'm using this in my personal project).