Waza_Be
Waza_Be

Reputation: 39558

SyncAdapter: Account created, requestSync OK, but setSyncAutomatically not Working

I just created an Account for my app.

I also followed all the steps from here: https://stackoverflow.com/a/5255360/327402

This is my code to get the sync by code

AccountManager am = AccountManager.get(this); 
Account[] accounts = am.getAccountsByType(ACCOUNT);
//Log.e("DEBUG", "Accounts: " + accounts.length);
if (accounts.length == 0) {
    Account account = new Account(getString(R.string.app_name), ACCOUNT);
    ContentResolver.setIsSyncable(account, AUTHORITY, 1);
    ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), 7200);
    ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
    if (am.addAccountExplicitly(account, "pass1", null))
        Log.i("DEBUG", "account Created: " + account.name + ", " + account.type);
    else
        Log.i("DEBUG", "addAccountExplicitly returned false");
    }
else{
    ContentResolver.requestSync(accounts[0], AUTHORITY, new Bundle());// THIS IS WORKING!!!
    }
}

So, everything looks correct and fine.

But unfortunately, I cannot get a periodic sync! When I open the settings, accounts, I see the account and the date and time is the time when I performed the sync by code, or manually.

Any idea on what I did wrong, or what I forgot?

Upvotes: 10

Views: 1400

Answers (2)

Cheticamp
Cheticamp

Reputation: 62831

Rewrite

I have put together a sample project on GitHub that demonstrates a working SyncAdapter. The project is here.

I have only tried this on an emulator with API 17 since I didn't want to wait around an hour or so (maybe longer now) for a sync to happen. I would suggest that you take this route as well.

On API 17, this demo will do a sync every 30 seconds or so. Everything runs out of the main activity with stub support classes: SyncAdapter, StubProvider, etc. The only thing the sync adapter does is to log a message to logcat that it has run.

I don't really see anything wrong with your code other than, perhaps, the order of the calls to set up the sync is incorrect. Take a look at the call order in the demo for an example of what works.

I hope you find this useful.

(I did this on Android Studio 3.0 Canary 5. I hope this is not an issue.)

Upvotes: 2

ApriOri
ApriOri

Reputation: 2688

One important thing to remember is that the periodic sync time is not guaranteed and may not be accurate, this is mentioned in the documentation:

Although these sync are scheduled at the specified frequency, it may take longer for it to actually be started if other syncs are ahead of it in the sync operation queue. This means that the actual start time may drift.

I would also try to create the account using AccountManager and not by creating new Account()

 manager.addAccountExplicitly(account, null, null)

And last thing, if this account was just created than you may want to force only the first sync right after you set the automatic sync settings:

 if (accountJustCreated) { 
   SyncAdapter.performSync();
 }

Upvotes: 0

Related Questions