Dan*_*son 15 android accountmanager android-syncadapter
我有一个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 (Exception e) {
Ln.e(e.getCause());
}
Run Code Online (Sandbox Code Playgroud)
我可以通过"设置"成功将帐户添加到"帐户管理器",但我必须手动为"设置"中的帐户启用同步,即使在模拟器上启用了后台数据和自动同步设置.如果我手动启用同步,则同步执行正常,默认情况下不启动.
Dan*_*son 30
ContentResolver.setIsSyncable(account, AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
Run Code Online (Sandbox Code Playgroud)
正如Blehi所说,在给定全局设置的情况下,将启动给定帐户的自动同步,启用"后台数据"和"自动同步".
为了防止背部到后端同步(从jcwenger)确保如果在任何方法
SyncAdapter.onPerformSync(...)调用ContentResolver.notifyChange(...)它使用ContentResolver.notifyChange(uri, observer, false)到标志通知不触发同步调用(第三个参数是syncToNetwork).
如果您正在使用为ContentProvider您执行插入/删除/更新,SyncAdapter则调用是有意义的,ContentResolver.notifyChange(...)以便在应用程序可见时,用户可以从中接收更新SyncAdapter,这意味着您将ContentProvider进行ContentResolver.notifyChange(...)拨打电话.为了使这个设置工作,我已经(在开发指南之后)将CALLER_IS_SYNC_ADAPTER查询参数添加到用于的每个URI SyncAdapter.将此方法添加ContentProvider到测试传入的URI
/**
* Determines if the given URI specifies that the request is coming from the sync adapter.
* @param uri the given URI
* @return true if the uri specifies that the request is coming from the sync adapter
*/
private boolean callerIsSyncAdapter(Uri uri) {
final String is_sync_adapter = uri.getQueryParameter(CALLER_IS_SYNC_ADAPTER);
return is_sync_adapter != null && !is_sync_adapter.equals("0");
}
Run Code Online (Sandbox Code Playgroud)
那么你可以做到
getContext().getContentResolver().notifyChange(uri, observer, !callerIsSyncAdapter(uri));
Run Code Online (Sandbox Code Playgroud)
无论何时您需要发送更改通知.
如果您想安排定期以一定频率执行同步(轮询服务器),请在ContentResolver.setSyncAutomatically(...)调用时添加此同步.
ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), frequency_in_seconds)
Run Code Online (Sandbox Code Playgroud)
and*_*sel 13
需要强调的是,似乎addPeriodicSync()需要 setSyncAutomatically(),即使文档说setSyncAutomatically()仅用于检测网络痒.
请注意,如果时间不到一分钟,则周期将更正为> 60秒.
您必须将帐户默认设置为可同步:
ContentResolver.setIsSyncable(account, AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
Run Code Online (Sandbox Code Playgroud)
我使用上面的2行,它正常工作.
| 归档时间: |
|
| 查看次数: |
13818 次 |
| 最近记录: |