我有一个SyncAdapter用于我的应用程序,以及AccountManager将我的应用程序帐户添加到Android帐户管理器.我向帐户管理器添加帐户的代码如下所示:
Bundle data = new Bundle(5);
data.putString(_PEOPLE_ID, people_id);
data.putString(_FIRST_NAME, first_name);
data.putString(_LAST_NAME, last_name);
data.putString(_PLAN, plan);
data.putString(_BIRTHDAY, birthday);
Account account = new Account(username, _ACCOUNT_TYPE);
try {
boolean created;
created = _account_manager.addAccountExplicitly(account,
_cryptography.encrypt(_SEED, password), data);
response.accountCreated(created);
_account_manager.setAuthToken(account, _TOKEN_TYPE, session_token);
_model.updateActiveAccount(people_id, username, password);
SharedPreferences.Editor settings = _settings.edit();
settings.putString(_ACCOUNT_TYPE, account.name);
settings.putString(_TOKEN_TYPE, session_token);
settings.commit();
// Tells the content provider that it can sync this account
ContentResolver.setIsSyncable(account, AUTHORITY, 1);
final Bundle extras = new Bundle(1);
extras.putBoolean(SYNC_EXTRAS_INITIALIZE, true);
ContentResolver.addPeriodicSync(account, AUTHORITY, extras, 900);
} catch …Run Code Online (Sandbox Code Playgroud) 我正在编写自己的ContentProvider,它将使用SyncAdapter同步到Web服务.
当同步适配器正在修改内容提供程序的数据时,问题发生在内部调用getContentResolver().notifyChange导致同步循环时,提供程序触发网络同步.
客户端应用程序执行修改时需要带有网络同步标志的notifyChange,但在同步适配器修改时应避免使用.
如何在内容提供者内部轻松判断客户端应用程序(应该在修改时触发网络同步)或同步适配器(不应触发网络同步)使用它.
目前我正在使用不同的CONTENT_URI(同步适配器使用CONTENT_URI_NO_SYNC访问数据,使用CONTENT_URI访问客户端应用程序)以便能够区分这两种类型的访问并相应地设置网络同步标志.