根据帐户设置调用的顺序不调用SyncAdapter

Jar*_*ith 16 android sync accounts

我的SyncAdapter遇到了一些奇怪的行为.

我第一次安装我的应用程序(使用adb卸载后),它会启动并创建一个帐户.根据某些语句的顺序(见下文),我的SyncAdapter onPerformSync()永远不会被调用; 我在"帐户和同步"下的帐户显示"同步进行中"图标无限期地旋转.如果我取消选中我应用帐户旁边的同步复选框,然后重新检查它,我的onPerformSync()会立即被调用.

这导致我的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);
Run Code Online (Sandbox Code Playgroud)

通过对相同语句的这种排序,一切都很完美:

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);
Run Code Online (Sandbox Code Playgroud)

我的猜测是我将SyncManager置于某种不一致的状态,因为它在创建帐户时立即触发了同步请求,而我正在改变它的配置.但是(几乎没有连贯的)文档没有提到任何随时调用这些函数的问题.

对于其他与SyncAdapter斗争的人而言,我发现除非你打电话,否则contentResolver.requestSync()永远不会触发你.SyncAdapter.onPerformSync()ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1);

有人可以解释这种行为吗?围绕账户和同步的文档至少可以说是不清楚的.

我在2.1仿真器上得到了这种行为,针对2.1 SDK进行了编译.

Jar*_*ith 7

我发现contentResolver.requestSync()除非你打电话,否则永远不会触发你的SyncAdapter.onPerformSync()ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1);.

有关我使用SyncAdapter的解决方案的详细说明,请参阅我的答案:

/sf/answers/841117721/