问题描述
我的 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.
顺便说一句,我发现 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!