问题描述
我的 SyncAdapter 遇到了一些有点奇怪的行为.
I've come across some slightly odd behaviour with my SyncAdapter.
我第一次安装我的应用程序时(使用 adb 卸载后),它会启动并创建一个帐户.根据某些语句的顺序(见下文),我的 SyncAdapter 的 onPerformSync() 将永远不会被调用;我在帐户和同步"下的帐户显示正在进行同步"图标无限期地旋转.如果我取消选中应用帐户旁边的同步复选框,然后重新选中它,我的 onPerformSync() 将立即被调用.
The first time I install my app (after uninstalling with adb), it launches and creates an account. Depending on the ordering of some statements (see below), my SyncAdapter's onPerformSync() will never be called; my account under "Accounts and sync" shows the "sync in progress" icon spinning indefinitely. If I then uncheck the sync checkbox next to my app's account, and then recheck it, my onPerformSync() is called immediately.
这会导致我的 SyncAdapter 永远不会被调用.同步正在进行"图标永远旋转,除非我取消选中,然后重新选中同步复选框:
final Account account = new Account(mUsername, getString(R.string.ACCOUNT_TYPE));
mAccountManager.addAccountExplicitly(account, mPassword, null);
ContentResolver.setSyncAutomatically(account, getString(R.string.CONTENT_AUTHORITY), true);
ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1);
使用相同语句的这种顺序,一切都完美无缺:
final Account account = new Account(mUsername, getString(R.string.ACCOUNT_TYPE));
ContentResolver.setSyncAutomatically(account, getString(R.string.CONTENT_AUTHORITY), true);
ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1);
mAccountManager.addAccountExplicitly(account, mPassword, null);
我的猜测是我将 SyncManager 置于某种不一致的状态,因为它在创建帐户时立即发出同步请求,并且我正在更改其配置.但是(勉强连贯的)文档没有提到在您喜欢时调用这些函数的任何问题.
My guess is I'm putting the SyncManager into some inconsistent state due to it firing off a sync request instantly on account creation, and I'm changing its config under its feet. But the (barely coherent) documentation doesn't mention any issues with calling these functions whenever you like.
作为其他人在 SyncAdapters 上苦苦挣扎的旁白,我发现 contentResolver.requestSync()
永远不会触发你的 SyncAdapter.onPerformSync()
除非你调用 ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1);
.
As an aside for others struggling with SyncAdapters, I found that contentResolver.requestSync()
will never trigger your SyncAdapter.onPerformSync()
unless you call ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1);
.
有人可以解释这种行为吗?至少可以说,有关帐户和同步的文档有些不清楚.
Could someone explain this behaviour? The documentation surrounding Accounts and Syncing is somewhat unclear to say the least.
我在 2.1 模拟器上得到了这种行为,针对 2.1 SDK 进行编译.
I'm getting this behaviour on the 2.1 Emulator, compiling against the 2.1 SDK.
推荐答案
我发现 contentResolver.requestSync()
永远不会触发你的 SyncAdapter.onPerformSync() 除非你调用 ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1);
.
I found that contentResolver.requestSync()
will never trigger your SyncAdapter.onPerformSync() unless you call ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1);
.
有关我使用 SyncAdapter 的解决方案的详细说明,请在此处查看我的答案:
For a detailed account of the solution I went with using SyncAdapter, see my answer here:
https://stackoverflow.com/a/12015967/988870
这篇关于根据帐户设置调用的顺序,不会调用 SyncAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!