I'm trying ty figure out how the syncAdapter works, I used the sampleSync Adapter as an example/starting point and I based my first test on it. The only difference is that I'm not working with the default contacts provider, but that I need one of my own.
This method is kinda the same as in the sampleSyncAdapter demo (in AccountAuthenticatorActivity), i've just added the periodic sync.
public void finishLogin(String authToken) {
Log.i(TAG, "finishLogin()");
final Account account = new Account(mUsername, "be.company.syncAdapterTest");
if(mRequestNewAccount) {
mAccountManager.addAccountExplicitly(account, mPassword, null);
ContentResolver.setIsSyncable(account, MY_AUTHORITY, 1);
Bundle params = new Bundle();
params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
ContentResolver.addPeriodicSync(account, MY_AUTHORITY, params, 30);
ContentResolver.setSyncAutomatically(account, MY_AUTHORITY, true);
ContentResolver.requestSync(account,MY_AUTHORITY,params);
} else {
mAccountManager.setPassword(account, mPassword);
}
final Intent intent = new Intent();
intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, "ACCOUNT_TEST");
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, "be.company.syncAdapterTest");
setAccountAuthenticatorResult(intent.getExtras());
setResult(RESULT_OK, intent);
finish();
}
In the perfomSync() method i have the following method:
@Override
public void onPerformSync(Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
Log.d(TAG, "onPerformSync() start");
// Testje
try {
final String authToken = mAccountManager.blockingGetAuthToken(account, "be.company.syncAdapterTest", NOTIFY_AUTH_FAILURE);
Log.d(TAG, SAPNetworkUtilities.getWeek(account, authToken, getRandomDate()));
} catch (OperationCanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthenticatorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "onPerformSync() end");
}
Here I just call a simple SAP webservice and show it in the log. Now I have following two questions:
- The SYNC is not started automatically when I add my Account in the settings. I need to go inside the account and check the checkbox to start the sync?
- The sync is not triggerd every 30 seconds in this example... Do I need to add something in the perfomSync() method in order to let the system know that the sync is done and that the next run can start?
At this moment i do not write the values in the contentProvider, just because i'm trying to figure out how the sync works in detail.
Currently I'm testing on the Android emulator.
Thanks in advance for your feedback.
Kind regards,
Robin