I have a SyncAdapter
for my app, and an AccountManager
to add my apps accounts to the Android Account Manager. My code for when I add an account to the Account Manager looks like:
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());
}
I can add the account to the Account Manager successfully through Settings, but I have to manually enable syncing for the account in Settings also, even though background data and sync automatically settings are enabled on the emulator. If I manually enable syncing, then the sync is performed fine, it just isn't started by default.