Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active April 16, 2017 20:20
Show Gist options
  • Save udacityandroid/7230489fb8cb3f46afee to your computer and use it in GitHub Desktop.
Save udacityandroid/7230489fb8cb3f46afee to your computer and use it in GitHub Desktop.
Methods for periodic syncing in the SunshineSyncAdapter
/**
* Helper method to schedule the sync adapter periodic execution
*/
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
Account account = getSyncAccount(context);
String authority = context.getString(R.string.content_authority);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// we can enable inexact timers in our periodic sync
SyncRequest request = new SyncRequest.Builder().
syncPeriodic(syncInterval, flexTime).
setSyncAdapter(account, authority).
setExtras(new Bundle()).build();
ContentResolver.requestSync(request);
} else {
ContentResolver.addPeriodicSync(account,
authority, new Bundle(), syncInterval);
}
}
private static void onAccountCreated(Account newAccount, Context context) {
/*
* Since we've created an account
*/
SunshineSyncAdapter.configurePeriodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME);
/*
* Without calling setSyncAutomatically, our periodic sync will not be enabled.
*/
ContentResolver.setSyncAutomatically(newAccount, context.getString(R.string.content_authority), true);
/*
* Finally, let's do a sync to get things started
*/
syncImmediately(context);
}
public static void initializeSyncAdapter(Context context) {
getSyncAccount(context);
}
@saeedAA
Copy link

saeedAA commented Feb 17, 2015

Error on line 12: "cannot resolve methode setExtras()".
removed .build() from line 11 to remove the bug.

@udacityandroid
Copy link
Author

Fixed, thank you!

@MacaveliMC
Copy link

Instructions twice say to add method initializeAdapter, but this gist has initializeSyncAdapter, and the bottom of the instructions says to add SunshineSyncAdapter.initializeSyncAdapter(this); to the end of MainActivity. Assuming it's supposed to be initializeSyncAdapter, where exactly at "the end of MainActivity" is it supposed to be? Did you mean the end of the onCreate mthod? I'm pretty sure you can't just throw that in the end of the class declaration outside of a method....?

@tarunkukreja003
Copy link

It is at the end of onCreate()

@francisrod01
Copy link

There's a problem in configurePeriodicSync() method to invokate SyncRequest() that is not public in android.content.SyncRequest().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment